From d89dafbfa070dc3594097c5be1c760cf726db32a Mon Sep 17 00:00:00 2001 From: Graham Jones Date: Sat, 30 Jan 2016 21:25:07 +0000 Subject: [PATCH] Made 'Open Pebble App' compatible with either pebble classic or pebble time app, and added an 'install watch app' button to the start-up screen to allow the user to install the watch app from OpenSeizureDetector. --- .../org/openseizuredetector/MainActivity.java | 31 ++++++------- .../uk/org/openseizuredetector/OsdUtil.java | 44 +++++++++++++++++++ .../openseizuredetector/StartupActivity.java | 20 +++++---- app/src/main/res/layout/startup_activity.xml | 31 ++++++++++--- .../main/res/menu/main_activity_actions.xml | 3 +- 5 files changed, 98 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/uk/org/openseizuredetector/MainActivity.java b/app/src/main/java/uk/org/openseizuredetector/MainActivity.java index 8f32c55..b056b0b 100644 --- a/app/src/main/java/uk/org/openseizuredetector/MainActivity.java +++ b/app/src/main/java/uk/org/openseizuredetector/MainActivity.java @@ -32,6 +32,7 @@ import android.content.pm.PackageManager; import android.content.SharedPreferences; import android.graphics.Color; import android.graphics.drawable.Drawable; +import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; @@ -156,13 +157,7 @@ public class MainActivity extends Activity { switch (item.getItemId()) { case R.id.action_launch_pebble_app: Log.v(TAG, "action_launch_pebble_app"); - try { - PackageManager pm = this.getPackageManager(); - Intent pebbleAppIntent = pm.getLaunchIntentForPackage("com.getpebble.android"); - this.startActivity(pebbleAppIntent); - } catch (Exception ex) { - Log.v(TAG, "exception starting pebble App " + ex.toString()); - } + mUtil.startPebbleApp(); return true; case R.id.action_accept_alarm: @@ -185,12 +180,14 @@ public class MainActivity extends Activity { mUtil.bindToServer(this, mConnection); } return true; + /* fault beep test does not work with fault timer, so disable test option. case R.id.action_test_fault_beep: Log.v(TAG, "action_test_fault_beep"); if (mConnection.mBound) { mConnection.mSdServer.faultWarningBeep(); } return true; + */ case R.id.action_test_alarm_beep: Log.v(TAG, "action_test_alarm_beep"); if (mConnection.mBound) { @@ -299,7 +296,7 @@ public class MainActivity extends Activity { if (mUtil.isServerRunning()) { tv = (TextView) findViewById(R.id.serverStatusTv); if (mConnection.mBound) - tv.setText("Server Running OK\n"+mConnection.mSdServer.mSdDataSourceName+" Data Source"); + tv.setText("Server Running OK\n" + mConnection.mSdServer.mSdDataSourceName + " Data Source"); tv.setBackgroundColor(okColour); tv.setTextColor(okTextColour); tv = (TextView) findViewById(R.id.serverIpTv); @@ -397,7 +394,7 @@ public class MainActivity extends Activity { long powerPc; if (mConnection.mSdServer.mSdData.alarmThresh != 0) powerPc = mConnection.mSdServer.mSdData.roiPower * 100 / - mConnection.mSdServer.mSdData.alarmThresh; + mConnection.mSdServer.mSdData.alarmThresh; else powerPc = 0; @@ -405,8 +402,8 @@ public class MainActivity extends Activity { if (mConnection.mSdServer.mSdData.specPower != 0 && mConnection.mSdServer.mSdData.alarmRatioThresh != 0) specPc = 100 * (mConnection.mSdServer.mSdData.roiPower * 10 / - mConnection.mSdServer.mSdData.specPower) / - mConnection.mSdServer.mSdData.alarmRatioThresh; + mConnection.mSdServer.mSdData.specPower) / + mConnection.mSdServer.mSdData.alarmRatioThresh; else specPc = 0; @@ -428,8 +425,10 @@ public class MainActivity extends Activity { pb.setMax(100); pb.setProgress((int) powerPc); pbDrawable = getResources().getDrawable(R.drawable.progress_bar_blue); - if (powerPc > 75) pbDrawable = getResources().getDrawable(R.drawable.progress_bar_yellow); - if (powerPc > 100) pbDrawable = getResources().getDrawable(R.drawable.progress_bar_red); + if (powerPc > 75) + pbDrawable = getResources().getDrawable(R.drawable.progress_bar_yellow); + if (powerPc > 100) + pbDrawable = getResources().getDrawable(R.drawable.progress_bar_red); //pb.getProgressDrawable().setColorFilter(colour, PorterDuff.Mode.SRC_IN); @@ -439,8 +438,10 @@ public class MainActivity extends Activity { pb.setMax(100); pb.setProgress((int) specPc); pbDrawable = getResources().getDrawable(R.drawable.progress_bar_blue); - if (specPc > 75) pbDrawable = getResources().getDrawable(R.drawable.progress_bar_yellow); - if (specPc > 100) pbDrawable = getResources().getDrawable(R.drawable.progress_bar_red); + if (specPc > 75) + pbDrawable = getResources().getDrawable(R.drawable.progress_bar_yellow); + if (specPc > 100) + pbDrawable = getResources().getDrawable(R.drawable.progress_bar_red); //pb.getProgressDrawable().setColorFilter(colour, PorterDuff.Mode.SRC_IN); pb.setProgressDrawable(pbDrawable); } else { // Not bound to server diff --git a/app/src/main/java/uk/org/openseizuredetector/OsdUtil.java b/app/src/main/java/uk/org/openseizuredetector/OsdUtil.java index 851f681..e7b5d6e 100644 --- a/app/src/main/java/uk/org/openseizuredetector/OsdUtil.java +++ b/app/src/main/java/uk/org/openseizuredetector/OsdUtil.java @@ -187,4 +187,48 @@ public class OsdUtil { Toast.LENGTH_LONG).show(); } + + /** + * Open Pebble or Pebble Time app. If it is not installed, open Play store so the user can install it. + */ + public void startPebbleApp() { + // first try to launch the original pebble app + Intent pebbleAppIntent; + PackageManager pm = mContext.getPackageManager(); + try { + pebbleAppIntent = pm.getLaunchIntentForPackage("com.getpebble.android"); + mContext.startActivity(pebbleAppIntent); + } catch (Exception ex1) { + // and if original pebble app fails, try Pebble Time app... + Log.v(TAG, "exception starting original pebble App - trying pebble time..." + ex1.toString()); + try { + pebbleAppIntent = pm.getLaunchIntentForPackage("com.getpebble.android.basalt"); + mContext.startActivity(pebbleAppIntent); + } catch (Exception ex2) { + // and if that fails, open play store so the user can install it: + Log.v(TAG, "exception starting Pebble Time App." + ex2.toString()); + this.showToast("Error Launching Pebble or Pebble Time App - Please make sure it is installed..."); + final String appPackageName = "com.getpebble.android.basalt"; + try { + // try using play store app. + mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName))); + } catch (android.content.ActivityNotFoundException anfe) { + // and if play store app is not installed, use browser to open app page. + mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName))); + } + } + } + + } + + /** + * Install the OpenSeizureDetector watch app onto the watch. + * based on https://forums.getpebble.com/discussion/13128/install-watch-app-pebble-store-from-android-companion-app + */ + public void installOsdWatchApp() { + Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("pebble://appstore/54d28a43e4d94c043f000008")); + myIntent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK ); + mContext.startActivity(myIntent); + } + } diff --git a/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java b/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java index fecc3cf..0053d89 100644 --- a/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java +++ b/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java @@ -91,14 +91,16 @@ public class StartupActivity extends Activity { @Override public void onClick(View view) { Log.v(TAG, "pebble button clicked"); - try { - PackageManager pm = getPackageManager(); - Intent pebbleAppIntent = pm.getLaunchIntentForPackage("com.getpebble.android"); - startActivity(pebbleAppIntent); - } catch (Exception ex) { - Log.v(TAG, "exception starting pebble App " + ex.toString()); - } + mUtil.startPebbleApp(); + } + }); + b = (Button)findViewById(R.id.installOsdAppButton); + b.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Log.v(TAG, "install Osd Watch App button clicked"); + mUtil.installOsdWatchApp(); } }); @@ -115,11 +117,13 @@ public class StartupActivity extends Activity { TextView tv = (TextView)findViewById(R.id.dataSourceTextView); tv.setText("DataSource = "+dataSourceName); - // disable pebble configuration button if we have not selected the pebble datasource + // disable pebble configuration buttons if we have not selected the pebble datasource if (!dataSourceName.equals("Pebble")) { Log.v(TAG, "Not Pebble Datasource - deactivating Pebble Button"); Button b = (Button) findViewById(R.id.pebbleButton); b.setEnabled(false); + b = (Button) findViewById(R.id.installOsdAppButton); + b.setEnabled(false); } if (mUtil.isServerRunning()) { diff --git a/app/src/main/res/layout/startup_activity.xml b/app/src/main/res/layout/startup_activity.xml index cb7ea5d..d6396b7 100644 --- a/app/src/main/res/layout/startup_activity.xml +++ b/app/src/main/res/layout/startup_activity.xml @@ -139,21 +139,38 @@ + android:layout_height="wrap_content">