1 2 package com.xxmassdeveloper.mpchartexample.notimportant; 3 4 import android.Manifest; 5 import android.content.pm.PackageManager; 6 import android.graphics.Typeface; 7 import android.os.Bundle; 8 import androidx.annotation.NonNull; 9 import androidx.annotation.Nullable; 10 import com.google.android.material.snackbar.Snackbar; 11 12 import androidx.appcompat.app.AppCompatActivity; 13 import androidx.core.app.ActivityCompat; 14 import android.view.View; 15 import android.widget.Toast; 16 17 import com.github.mikephil.charting.charts.Chart; 18 import com.xxmassdeveloper.mpchartexample.R; 19 20 /** 21 * Base class of all Activities of the Demo Application. 22 * 23 * @author Philipp Jahoda 24 */ 25 public abstract class DemoBase extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback { 26 27 protected final String[] months = new String[] { 28 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec" 29 }; 30 31 protected final String[] parties = new String[] { 32 "Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G", "Party H", 33 "Party I", "Party J", "Party K", "Party L", "Party M", "Party N", "Party O", "Party P", 34 "Party Q", "Party R", "Party S", "Party T", "Party U", "Party V", "Party W", "Party X", 35 "Party Y", "Party Z" 36 }; 37 38 private static final int PERMISSION_STORAGE = 0; 39 40 protected Typeface tfRegular; 41 protected Typeface tfLight; 42 43 @Override onCreate(@ullable Bundle savedInstanceState)44 protected void onCreate(@Nullable Bundle savedInstanceState) { 45 super.onCreate(savedInstanceState); 46 47 tfRegular = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); 48 tfLight = Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf"); 49 } 50 getRandom(float range, float start)51 protected float getRandom(float range, float start) { 52 return (float) (Math.random() * range) + start; 53 } 54 55 @Override onBackPressed()56 public void onBackPressed() { 57 super.onBackPressed(); 58 overridePendingTransition(R.anim.move_left_in_activity, R.anim.move_right_out_activity); 59 } 60 61 @Override onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)62 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 63 if (requestCode == PERMISSION_STORAGE) { 64 if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 65 saveToGallery(); 66 } else { 67 Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT) 68 .show(); 69 } 70 } 71 } 72 requestStoragePermission(View view)73 protected void requestStoragePermission(View view) { 74 if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 75 Snackbar.make(view, "Write permission is required to save image to gallery", Snackbar.LENGTH_INDEFINITE) 76 .setAction(android.R.string.ok, new View.OnClickListener() { 77 @Override 78 public void onClick(View v) { 79 ActivityCompat.requestPermissions(DemoBase.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_STORAGE); 80 } 81 }).show(); 82 } else { 83 Toast.makeText(getApplicationContext(), "Permission Required!", Toast.LENGTH_SHORT) 84 .show(); 85 ActivityCompat.requestPermissions(DemoBase.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_STORAGE); 86 } 87 } 88 saveToGallery(Chart chart, String name)89 protected void saveToGallery(Chart chart, String name) { 90 if (chart.saveToGallery(name + "_" + System.currentTimeMillis(), 70)) 91 Toast.makeText(getApplicationContext(), "Saving SUCCESSFUL!", 92 Toast.LENGTH_SHORT).show(); 93 else 94 Toast.makeText(getApplicationContext(), "Saving FAILED!", Toast.LENGTH_SHORT) 95 .show(); 96 } 97 saveToGallery()98 protected abstract void saveToGallery(); 99 } 100