From 4cd2220d5145aaaf565adf5691fa82b0f45fdeed Mon Sep 17 00:00:00 2001 From: Graham Jones Date: Wed, 23 Jan 2019 06:25:34 +0000 Subject: [PATCH] Changed lots of log entries to debug start-stop problems and (I think) fixed the problem with permissions on Android 8 - alpha test version of V2.6 --- app/src/main/AndroidManifest.xml | 4 +- .../org/openseizuredetector/MainActivity.java | 5 +- .../OsdUncaughtExceptionHandler.java | 2 +- .../uk/org/openseizuredetector/OsdUtil.java | 49 ++++++++++++++++++- .../org/openseizuredetector/PrefActivity.java | 20 ++++---- .../SdDataSourceGarmin.java | 6 +-- .../SdDataSourcePebble.java | 9 ++-- .../uk/org/openseizuredetector/SdServer.java | 21 ++++---- .../openseizuredetector/StartupActivity.java | 17 ++++--- 9 files changed, 95 insertions(+), 38 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d13dd84..0672e07 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,7 +2,9 @@ + android:versionName="2.6.0" + android:allowBackup="false" + > diff --git a/app/src/main/java/uk/org/openseizuredetector/MainActivity.java b/app/src/main/java/uk/org/openseizuredetector/MainActivity.java index b80a21d..7f07f08 100644 --- a/app/src/main/java/uk/org/openseizuredetector/MainActivity.java +++ b/app/src/main/java/uk/org/openseizuredetector/MainActivity.java @@ -86,7 +86,7 @@ public class MainActivity extends AppCompatActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - Log.v(TAG,"onCreate()"); + Log.i(TAG,"onCreate()"); // Set our custom uncaught exception handler to report issues. Thread.setDefaultUncaughtExceptionHandler(new OsdUncaughtExceptionHandler(MainActivity.this)); @@ -187,6 +187,7 @@ public class MainActivity extends AppCompatActivity { Log.i(TAG, "Starting Server"); startServer(); // and bind to it so we can see its data + Log.i(TAG, "Binding to Server"); mUtil.bindToServer(this, mConnection); } return true; @@ -269,7 +270,7 @@ public class MainActivity extends AppCompatActivity { String versionName = mUtil.getAppVersionName(); tv.setText("OpenSeizureDetector Android App Version " + versionName); - Log.v(TAG,"onStart() - binding to server"); + Log.i(TAG,"onStart() - binding to server"); mUtil.writeToSysLogFile("MainActivity.onStart - Binding to Server"); mUtil.bindToServer(this, mConnection); diff --git a/app/src/main/java/uk/org/openseizuredetector/OsdUncaughtExceptionHandler.java b/app/src/main/java/uk/org/openseizuredetector/OsdUncaughtExceptionHandler.java index 5f6a397..3834a92 100644 --- a/app/src/main/java/uk/org/openseizuredetector/OsdUncaughtExceptionHandler.java +++ b/app/src/main/java/uk/org/openseizuredetector/OsdUncaughtExceptionHandler.java @@ -164,7 +164,7 @@ public class OsdUncaughtExceptionHandler implements Thread.UncaughtExceptionHand "\n"+errorContent.toString()); Dialog dialog = builder.create(); dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); - dialog.show(); + //dialog.show(); Looper.loop(); } }.start(); diff --git a/app/src/main/java/uk/org/openseizuredetector/OsdUtil.java b/app/src/main/java/uk/org/openseizuredetector/OsdUtil.java index 7861872..80a336e 100644 --- a/app/src/main/java/uk/org/openseizuredetector/OsdUtil.java +++ b/app/src/main/java/uk/org/openseizuredetector/OsdUtil.java @@ -24,6 +24,7 @@ */ package uk.org.openseizuredetector; +import android.Manifest; import android.app.Activity; import android.app.ActivityManager; import android.content.ComponentName; @@ -57,6 +58,8 @@ import android.os.UserHandle; import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; import android.text.format.Time; import android.util.Log; import android.view.MenuItem; @@ -80,11 +83,16 @@ import java.util.concurrent.RunnableFuture; * OsdUtil - OpenSeizureDetector Utilities * Deals with starting and stopping the background service and binding to it to receive data. */ -public class OsdUtil { +public class OsdUtil implements ActivityCompat.OnRequestPermissionsResultCallback { private final String SYSLOG = "SysLog"; private final String ALARMLOG = "AlarmLog"; private final String DATALOG = "DataLog"; + private final String[] REQUIRED_PERMISSIONS = { + Manifest.permission.SEND_SMS, + Manifest.permission.WRITE_EXTERNAL_STORAGE, + }; + /** * Based on http://stackoverflow.com/questions/7440473/android-how-to-check-if-the-intent-service-is-still-running-or-has-stopped-running */ @@ -94,6 +102,7 @@ public class OsdUtil { private boolean mLogAlarms = true; private boolean mLogSystem = true; private boolean mLogData = true; + private boolean mPermissionsRequested = false; public OsdUtil(Context context, Handler handler) { mContext = context; @@ -404,4 +413,42 @@ public class OsdUtil { } } + public boolean arePermissionsOK() { + boolean allOk = true; + Log.v(TAG,"arePermissionsOK"); + for (int i = 0; i< REQUIRED_PERMISSIONS.length; i++) { + if (ContextCompat.checkSelfPermission(mContext, REQUIRED_PERMISSIONS[i]) + != PackageManager.PERMISSION_GRANTED) { + Log.i(TAG, REQUIRED_PERMISSIONS[i] + " Permission Not Granted"); + allOk = false; + } + } + return allOk; + } + + public void requestPermissions(Activity activity) { + if (mPermissionsRequested) { + Log.i(TAG,"requestPermissions() - request already sent - not doing anything"); + } else { + Log.i(TAG, "requestPermissions() - requesting permissions"); + for (int i = 0; i < REQUIRED_PERMISSIONS.length; i++) { + if (ActivityCompat.shouldShowRequestPermissionRationale(activity, + REQUIRED_PERMISSIONS[i])) { + Log.i(TAG, "shouldShowRationale for permission" + REQUIRED_PERMISSIONS[i]); + } + } + ActivityCompat.requestPermissions(activity, + REQUIRED_PERMISSIONS, + 42); + mPermissionsRequested = true; + } + + + } + + @Override + public void onRequestPermissionsResult(int requestCode, + String permissions[], int[] grantResults) { + Log.i(TAG,"onequestPermissionsResult - Permission" + permissions + " = " + grantResults); + } } diff --git a/app/src/main/java/uk/org/openseizuredetector/PrefActivity.java b/app/src/main/java/uk/org/openseizuredetector/PrefActivity.java index 721c122..9de08da 100644 --- a/app/src/main/java/uk/org/openseizuredetector/PrefActivity.java +++ b/app/src/main/java/uk/org/openseizuredetector/PrefActivity.java @@ -70,7 +70,7 @@ public class PrefActivity extends PreferenceActivity implements SharedPreference SharedPreferences SP = PreferenceManager .getDefaultSharedPreferences(this.getApplicationContext()); String dataSourceStr = SP.getString("DataSource", "Pebble"); - Log.v(TAG, "onBuildHeaders DataSource = " + dataSourceStr); + Log.i(TAG, "onBuildHeaders DataSource = " + dataSourceStr); //Boolean cameraEnabled = SP.getBoolean("UseIpCamera", false); //Log.v(TAG, "onBuildHeaders cameraEnabled = " + cameraEnabled); @@ -111,12 +111,12 @@ public class PrefActivity extends PreferenceActivity implements SharedPreference super.onStart(); mUtil.writeToSysLogFile("PrefActvity.onStart()"); invalidateHeaders(); - Log.v(TAG, "onStart()"); + Log.i(TAG, "onStart()"); } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) { - Log.v(TAG, "SharedPreference " + s + " Changed."); + Log.i(TAG, "SharedPreference " + s + " Changed."); //mUtil.showToast("Shared Preference " + s + " Changed."); mPrefChanged = true; } @@ -126,7 +126,7 @@ public class PrefActivity extends PreferenceActivity implements SharedPreference public void onResume() { super.onResume(); mUtil.writeToSysLogFile("PrefActvity.onResume()"); - Log.v(TAG, "onResume()"); + Log.i(TAG, "onResume()"); SharedPreferences SP = PreferenceManager .getDefaultSharedPreferences(getBaseContext()); SP.registerOnSharedPreferenceChangeListener(this); @@ -135,6 +135,7 @@ public class PrefActivity extends PreferenceActivity implements SharedPreference @Override protected void onPause() { super.onPause(); + Log.i(TAG, "onPause()"); mUtil.writeToSysLogFile("PrefActvity.onPause()"); SharedPreferences SP = PreferenceManager .getDefaultSharedPreferences(getBaseContext()); @@ -142,12 +143,13 @@ public class PrefActivity extends PreferenceActivity implements SharedPreference } @Override - protected void onDestroy() { - super.onDestroy(); - mUtil.writeToSysLogFile("PrefActvity.onDestroy()"); - Log.v(TAG, "onDestroy. mPrefChanged=" + mPrefChanged); + protected void onStop() { + super.onStop(); + mUtil.writeToSysLogFile("PrefActvity.onStop()"); + Log.i(TAG, "onStop. mPrefChanged=" + mPrefChanged); if (mPrefChanged) { - mUtil.writeToSysLogFile("PrefActvity.onDestroy() - settings changed - re-starting...."); + Log.i(TAG,"PrefActivity.onStop() - settings changed - restarting server"); + mUtil.writeToSysLogFile("PrefActvity.onStop() - settings changed - re-starting server...."); mUtil.showToast("Settings Changed - re-starting OpenSeizureDetector...."); Intent intent = new Intent(getApplicationContext(), StartupActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); diff --git a/app/src/main/java/uk/org/openseizuredetector/SdDataSourceGarmin.java b/app/src/main/java/uk/org/openseizuredetector/SdDataSourceGarmin.java index c4487fb..90e3eb4 100644 --- a/app/src/main/java/uk/org/openseizuredetector/SdDataSourceGarmin.java +++ b/app/src/main/java/uk/org/openseizuredetector/SdDataSourceGarmin.java @@ -114,7 +114,7 @@ public class SdDataSourceGarmin extends SdDataSource { public SdDataSourceGarmin(Context context, Handler handler, SdDataReceiver sdDataReceiver) { super(context, handler, sdDataReceiver); - mName = "NetworkPassive"; + mName = "Garmin"; // Set default settings from XML files (mContext is set by super(). PreferenceManager.setDefaultValues(mContext, R.xml.network_passive_datasource_prefs, true); @@ -126,7 +126,7 @@ public class SdDataSourceGarmin extends SdDataSource { * make sure any changes to preferences are taken into account. */ public void start() { - Log.v(TAG, "start()"); + Log.i(TAG, "start()"); mUtil.writeToSysLogFile("SdDataSourceGarmin.start()"); updatePrefs(); // Start timer to check status of watch regularly. @@ -188,7 +188,7 @@ public class SdDataSourceGarmin extends SdDataSource { * Stop the datasource from updating */ public void stop() { - Log.v(TAG, "stop()"); + Log.i(TAG, "stop()"); mUtil.writeToSysLogFile("SdDataSourceGarmin.stop()"); try { // Stop the status timer diff --git a/app/src/main/java/uk/org/openseizuredetector/SdDataSourcePebble.java b/app/src/main/java/uk/org/openseizuredetector/SdDataSourcePebble.java index a98ced5..edf443c 100644 --- a/app/src/main/java/uk/org/openseizuredetector/SdDataSourcePebble.java +++ b/app/src/main/java/uk/org/openseizuredetector/SdDataSourcePebble.java @@ -171,7 +171,7 @@ public class SdDataSourcePebble extends SdDataSource { // use a timer to check the status of the pebble app on the same frequency // as we get app data. if (mStatusTimer == null) { - Log.v(TAG, "start(): starting status timer"); + Log.v(TAG, "start(): starting status timer with period "+mDataUpdatePeriod*1000 + " ms"); mUtil.writeToSysLogFile("SdDataSourcePebble.start() - starting status timer"); mStatusTimer = new Timer(); mStatusTimer.schedule(new TimerTask() { @@ -190,7 +190,7 @@ public class SdDataSourcePebble extends SdDataSource { getPebbleSdSettings(); if (mSettingsTimer == null) { Log.v(TAG, "start(): starting settings timer"); - mUtil.writeToSysLogFile("SdDataSourcePebble.start() - starting settings timer"); + mUtil.writeToSysLogFile("SdDataSourcePebble.start() - starting settings timer with period "+1000*mSettingsPeriod); mSettingsTimer = new Timer(); mSettingsTimer.schedule(new TimerTask() { @Override @@ -244,6 +244,8 @@ public class SdDataSourcePebble extends SdDataSource { * - defined in res/xml/SdDataSourcePebblePrefs.xml */ public void updatePrefs() { + String prefStr = "null"; + Log.v(TAG, "updatePrefs()"); //mUtil.writeToSysLogFile("SdDataSourcePebble.updatePrefs()"); SharedPreferences SP = PreferenceManager @@ -273,7 +275,6 @@ public class SdDataSourcePebble extends SdDataSource { // Watch Settings - String prefStr; prefStr = SP.getString("PebbleDebug", "SET_FROM_XML"); mDebug = (short) Integer.parseInt(prefStr); @@ -347,7 +348,7 @@ public class SdDataSourcePebble extends SdDataSource { Log.v(TAG, "updatePrefs() FallWindow = " + mFallWindow); } catch (Exception ex) { - Log.v(TAG, "updatePrefs() - Problem parsing preferences!"); + Log.v(TAG, "updatePrefs() - Problem parsing preferences! - prefStr="+prefStr); mUtil.writeToSysLogFile("SdDataSourcePebble.updatePrefs() - ERROR "+ex.toString()); Toast toast = Toast.makeText(mContext, "Problem Parsing Preferences - Something won't work - Please go back to Settings and correct it!", Toast.LENGTH_SHORT); toast.show(); diff --git a/app/src/main/java/uk/org/openseizuredetector/SdServer.java b/app/src/main/java/uk/org/openseizuredetector/SdServer.java index 14fedcc..0255b8d 100644 --- a/app/src/main/java/uk/org/openseizuredetector/SdServer.java +++ b/app/src/main/java/uk/org/openseizuredetector/SdServer.java @@ -139,14 +139,14 @@ public class SdServer extends Service implements SdDataReceiver, SdLocationRecei */ public SdServer() { super(); - Log.v(TAG, "SdServer Created"); + Log.i(TAG, "SdServer Created"); mSdData = new SdData(); mToneGenerator = new ToneGenerator(AudioManager.STREAM_ALARM, 100); } @Override public IBinder onBind(Intent intent) { - Log.v(TAG, "sdServer.onBind()"); + Log.i(TAG, "sdServer.onBind()"); return mBinder; } @@ -163,7 +163,7 @@ public class SdServer extends Service implements SdDataReceiver, SdLocationRecei */ @Override public void onCreate() { - Log.v(TAG, "onCreate()"); + Log.i(TAG, "onCreate()"); mHandler = new Handler(); mUtil = new OsdUtil(getApplicationContext(), mHandler); mUtil.writeToSysLogFile("SdServer.onCreate()"); @@ -186,7 +186,7 @@ public class SdServer extends Service implements SdDataReceiver, SdLocationRecei */ @Override public int onStartCommand(Intent intent, int flags, int startId) { - Log.v(TAG, "onStartCommand() - SdServer service starting"); + Log.i(TAG, "onStartCommand() - SdServer service starting"); mUtil.writeToSysLogFile("SdServer.onStartCommand()"); // Update preferences. @@ -216,10 +216,11 @@ public class SdServer extends Service implements SdDataReceiver, SdLocationRecei mSdDataSource = new SdDataSourceGarmin(this.getApplicationContext(), mHandler, this); break; default: - Log.v(TAG, "Datasource " + mSdDataSourceName + " not recognised - Exiting"); + Log.e(TAG, "Datasource " + mSdDataSourceName + " not recognised - Defaulting to Pebble"); mUtil.writeToSysLogFile("SdServer.onStartCommand() - Datasource " + mSdDataSourceName + " not recognised - exiting"); - mUtil.showToast("Datasource " + mSdDataSourceName + " not recognised - Exiting"); - return 1; + mUtil.showToast("Datasource " + mSdDataSourceName + " not recognised - Defaulting to Pebble"); + mUtil.writeToSysLogFile("SdServer.onStartCommand() - creating SdDataSourcePebble"); + mSdDataSource = new SdDataSourcePebble(this.getApplicationContext(), mHandler, this); } if (mSMSAlarm) { @@ -295,7 +296,7 @@ public class SdServer extends Service implements SdDataReceiver, SdLocationRecei @Override public void onDestroy() { - Log.v(TAG, "onDestroy(): SdServer Service stopping"); + Log.i(TAG, "onDestroy(): SdServer Service stopping"); mUtil.writeToSysLogFile("SdServer.onDestroy() - releasing wakelock"); // release the wake lock to allow CPU to sleep and reduce // battery drain. @@ -314,7 +315,7 @@ public class SdServer extends Service implements SdDataReceiver, SdLocationRecei } if (mSdDataSource != null) { - Log.v(TAG, "stopping mSdDataSource"); + Log.i(TAG, "stopping mSdDataSource"); mUtil.writeToSysLogFile("SdServer.onDestroy() - stopping mSdDataSource"); mSdDataSource.stop(); } else { @@ -574,7 +575,7 @@ public class SdServer extends Service implements SdDataReceiver, SdLocationRecei // Called by SdDataSource when a fault condition is detected. public void onSdDataFault(SdData sdData) { - Log.i(TAG, "onSdDataFault()"); + Log.v(TAG, "onSdDataFault()"); mSdData = sdData; mSdData.alarmState = 4; // set fault alarm state. mSdData.alarmStanding = false; diff --git a/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java b/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java index ccc02b1..5654667 100644 --- a/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java +++ b/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java @@ -164,6 +164,7 @@ public class StartupActivity extends Activity { } }); + mConnection = new SdServiceConnection(this); } @Override @@ -198,16 +199,17 @@ public class StartupActivity extends Activity { } if (mUtil.isServerRunning()) { - Log.v(TAG, "onStart() - server running - stopping it"); + Log.i(TAG, "onStart() - server running - stopping it"); mUtil.writeToSysLogFile("StartupActivity.onStart() - server already running - stopping it."); mUtil.stopServer(); } mUtil.writeToSysLogFile("StartupActivity.onStart() - starting server"); + Log.i(TAG,"onStart() - starting server"); mUtil.startServer(); // Bind to the service. + Log.i(TAG,"onStart() - binding to server"); mUtil.writeToSysLogFile("StartupActivity.onStart() - binding to server"); - mConnection = new SdServiceConnection(this); mUtil.bindToServer(this, mConnection); // Check to see if this is the first time the app has been run, and display welcome dialog if it is. @@ -229,7 +231,7 @@ public class StartupActivity extends Activity { @Override protected void onStop() { super.onStop(); - Log.i(TAG, "onStop()"); + Log.i(TAG, "onStop() - unbinding from server"); mUtil.writeToSysLogFile("StartupActivity.onStop() - unbinding from server"); mUtil.unbindFromServer(this, mConnection); mUiTimer.cancel(); @@ -250,21 +252,22 @@ public class StartupActivity extends Activity { Log.v(TAG,"serverStatusRunnable()"); - // Service Running + // Settings ok tv = (TextView) findViewById(R.id.textItem1); pb = (ProgressBar) findViewById(R.id.progressBar1); - if (mUtil.isServerRunning()) { - tv.setText("Background Service Running OK"); + if (mUtil.arePermissionsOK()) { + tv.setText("App Permissions OK"); tv.setBackgroundColor(okColour); tv.setTextColor(okTextColour); pb.setIndeterminateDrawable(getResources().getDrawable(R.drawable.start_server)); pb.setProgressDrawable(getResources().getDrawable(R.drawable.start_server)); } else { - tv.setText("Waiting for Background Service..."); + tv.setText("Problem with App Permissions"); tv.setBackgroundColor(alarmColour); tv.setTextColor(alarmTextColour); pb.setIndeterminate(true); allOk = false; + mUtil.requestPermissions(StartupActivity.this); } // Are we Bound to the Service