|
| 1 | +package org.sofwerx.torgi.ui; |
| 2 | + |
| 3 | +import android.Manifest; |
| 4 | +import android.app.Activity; |
| 5 | +import android.app.AlertDialog; |
| 6 | +import android.content.ActivityNotFoundException; |
| 7 | +import android.content.ComponentName; |
| 8 | +import android.content.Context; |
| 9 | +import android.content.DialogInterface; |
| 10 | +import android.content.Intent; |
| 11 | +import android.content.ServiceConnection; |
| 12 | +import android.content.SharedPreferences; |
| 13 | +import android.content.pm.PackageManager; |
| 14 | +import android.location.Location; |
| 15 | +import android.net.Uri; |
| 16 | +import android.os.Build; |
| 17 | +import android.os.Bundle; |
| 18 | +import android.os.IBinder; |
| 19 | +import android.os.PowerManager; |
| 20 | +import android.preference.PreferenceManager; |
| 21 | +import android.provider.Settings; |
| 22 | +import android.support.v4.app.ActivityCompat; |
| 23 | +import android.support.v4.content.ContextCompat; |
| 24 | +import android.util.Log; |
| 25 | +import android.widget.Toast; |
| 26 | + |
| 27 | +import org.sofwerx.torgi.R; |
| 28 | +import org.sofwerx.torgi.gnss.LatLng; |
| 29 | +import org.sofwerx.torgi.listener.GnssMeasurementListener; |
| 30 | +import org.sofwerx.torgi.service.TorgiService; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | + |
| 34 | +public abstract class AbstractTORGIActivity extends Activity { |
| 35 | + protected static final int REQUEST_DISABLE_BATTERY_OPTIMIZATION = 401; |
| 36 | + protected final static String TAG = "TORGI.monitor"; |
| 37 | + private final static String PREF_BATTERY_OPT_IGNORE = "nvroptbat"; |
| 38 | + private final static int PERM_REQUEST_CODE = 1; |
| 39 | + private boolean serviceBound = false; |
| 40 | + private TorgiService torgiService = null; |
| 41 | + protected boolean permissionsPassed = false; |
| 42 | + |
| 43 | + protected void onCreate(Bundle savedInstanceState) { |
| 44 | + super.onCreate(savedInstanceState); |
| 45 | + setContentView(getLayout()); |
| 46 | + permissionsPassed = checkPermissions(); |
| 47 | + if (permissionsPassed) |
| 48 | + startService(); |
| 49 | + openBatteryOptimizationDialogIfNeeded(); |
| 50 | + }; |
| 51 | + |
| 52 | + protected abstract int getLayout(); |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onResume() { |
| 56 | + super.onResume(); |
| 57 | + if (this instanceof GnssMeasurementListener) { |
| 58 | + if (serviceBound && (torgiService != null)) |
| 59 | + torgiService.setListener((GnssMeasurementListener)this); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onPause() { |
| 65 | + if (torgiService != null) |
| 66 | + torgiService.setListener(null); |
| 67 | + super.onPause(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onStop() { |
| 72 | + super.onStop(); |
| 73 | + if (serviceBound && (torgiService != null)) { |
| 74 | + try { |
| 75 | + unbindService(mConnection); |
| 76 | + } catch (Exception e) { |
| 77 | + e.printStackTrace(); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + protected ServiceConnection mConnection = new ServiceConnection() { |
| 83 | + @Override |
| 84 | + public void onServiceConnected(ComponentName className, IBinder service) { |
| 85 | + Log.d(TAG,"MdxService bound to this activity"); |
| 86 | + TorgiService.TorgiBinder binder = (TorgiService.TorgiBinder) service; |
| 87 | + torgiService = binder.getService(); |
| 88 | + serviceBound = true; |
| 89 | + onTorgiServiceConnected(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void onServiceDisconnected(ComponentName arg0) { |
| 94 | + serviceBound = false; |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + private void onTorgiServiceConnected() { |
| 99 | + if (this instanceof GnssMeasurementListener) |
| 100 | + torgiService.setListener((GnssMeasurementListener)this); |
| 101 | + } |
| 102 | + |
| 103 | + private void startService() { |
| 104 | + if (serviceBound) |
| 105 | + torgiService.start(); |
| 106 | + else { |
| 107 | + startService(new Intent(this, TorgiService.class)); |
| 108 | + Intent intent = new Intent(this, TorgiService.class); |
| 109 | + bindService(intent, mConnection, Context.BIND_AUTO_CREATE); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Request battery optimization exception so that the system doesn't throttle back our app |
| 115 | + */ |
| 116 | + private void openBatteryOptimizationDialogIfNeeded() { |
| 117 | + if (isOptimizingBattery() && isAllowAskAboutBattery()) { |
| 118 | + AlertDialog.Builder builder = new AlertDialog.Builder(this); |
| 119 | + builder.setTitle(R.string.enable_battery_optimization); |
| 120 | + builder.setMessage(R.string.battery_optimizations_narrative); |
| 121 | + builder.setPositiveButton(R.string.battery_optimize_yes, (dialog, which) -> { |
| 122 | + Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); |
| 123 | + Uri uri = Uri.parse("package:" + getPackageName()); |
| 124 | + intent.setData(uri); |
| 125 | + try { |
| 126 | + startActivityForResult(intent, REQUEST_DISABLE_BATTERY_OPTIMIZATION); |
| 127 | + } catch (ActivityNotFoundException e) { |
| 128 | + Toast.makeText(this, R.string.does_not_support_battery_optimization, Toast.LENGTH_SHORT).show(); |
| 129 | + } |
| 130 | + }); |
| 131 | + builder.setOnDismissListener(dialog -> setNeverAskBatteryOptimize()); |
| 132 | + final AlertDialog dialog = builder.create(); |
| 133 | + dialog.setCanceledOnTouchOutside(false); |
| 134 | + dialog.show(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + protected boolean isOptimizingBattery() { |
| 139 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 140 | + final PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); |
| 141 | + return pm != null && !pm.isIgnoringBatteryOptimizations(getPackageName()); |
| 142 | + } else |
| 143 | + return false; |
| 144 | + } |
| 145 | + |
| 146 | + private boolean isAllowAskAboutBattery() { |
| 147 | + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); |
| 148 | + return !prefs.getBoolean(PREF_BATTERY_OPT_IGNORE,false); |
| 149 | + } |
| 150 | + |
| 151 | + private void setNeverAskBatteryOptimize() { |
| 152 | + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); |
| 153 | + SharedPreferences.Editor edit = prefs.edit(); |
| 154 | + edit.putBoolean(PREF_BATTERY_OPT_IGNORE,true); |
| 155 | + edit.apply(); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public void onActivityResult(int requestCode, int resultCode, final Intent data) { |
| 160 | + super.onActivityResult(requestCode, resultCode, data); |
| 161 | + switch (requestCode) { |
| 162 | + case REQUEST_DISABLE_BATTERY_OPTIMIZATION: |
| 163 | + setNeverAskBatteryOptimize(); |
| 164 | + break; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { |
| 170 | + if (requestCode == PERM_REQUEST_CODE) { |
| 171 | + if (checkPermissions()) { |
| 172 | + permissionsPassed = true; |
| 173 | + startService(); |
| 174 | + } else { |
| 175 | + Toast.makeText(this, "Both Location and Storage permissions are needed", Toast.LENGTH_LONG).show(); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Returns true once all required permissions are granted, otherwise returns false and requests the |
| 182 | + * required permission |
| 183 | + * @return |
| 184 | + */ |
| 185 | + private boolean checkPermissions() { |
| 186 | + ArrayList<String> needed = new ArrayList<>(); |
| 187 | + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) |
| 188 | + needed.add(Manifest.permission.ACCESS_FINE_LOCATION); |
| 189 | + if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) |
| 190 | + needed.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); |
| 191 | + if (needed.isEmpty()) |
| 192 | + return true; |
| 193 | + else { |
| 194 | + String[] perms = new String[needed.size()]; |
| 195 | + perms = new String[perms.length]; |
| 196 | + for (int i=0;i<perms.length;i++) { |
| 197 | + perms[i] = needed.get(i); |
| 198 | + } |
| 199 | + ActivityCompat.requestPermissions(this, perms, PERM_REQUEST_CODE); |
| 200 | + return false; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public void onBackPressed() { |
| 206 | + new AlertDialog.Builder(this) |
| 207 | + .setTitle(R.string.quit_torgi) |
| 208 | + .setMessage(R.string.quit_torgi_narrative) |
| 209 | + .setNegativeButton(R.string.quit_yes, new DialogInterface.OnClickListener() { |
| 210 | + @Override |
| 211 | + public void onClick(DialogInterface dialog, int which) { |
| 212 | + if (serviceBound && (torgiService != null)) |
| 213 | + torgiService.shutdown(); |
| 214 | + AbstractTORGIActivity.this.finish(); |
| 215 | + } |
| 216 | + }) |
| 217 | + .setPositiveButton(R.string.quit_run_in_background, new DialogInterface.OnClickListener() { |
| 218 | + public void onClick(DialogInterface arg0, int arg1) { |
| 219 | + AbstractTORGIActivity.this.finish(); |
| 220 | + } |
| 221 | + }).create().show(); |
| 222 | + } |
| 223 | +} |
0 commit comments