From 114effd7f94928b21e4f042df47d08fce739307e Mon Sep 17 00:00:00 2001 From: Guillaume Perrot Date: Thu, 4 May 2017 13:52:52 -0700 Subject: [PATCH 1/2] Add Push to jCenter build flavour and use 0.8.0 SDK --- apps/sasquatch/build.gradle | 9 ++- .../features/PushListenerHelper.java | 9 --- .../sasquatch/activities/MainActivity.java | 66 +++++++++++-------- .../activities/SettingsActivity.java | 31 ++------- .../features/PushListenerHelper.java | 42 ------------ versions.gradle | 2 +- 6 files changed, 52 insertions(+), 107 deletions(-) delete mode 100644 apps/sasquatch/src/jcenterDependency/java/com/microsoft/azure/mobile/sasquatch/features/PushListenerHelper.java delete mode 100644 apps/sasquatch/src/projectDependency/java/com/microsoft/azure/mobile/sasquatch/features/PushListenerHelper.java diff --git a/apps/sasquatch/build.gradle b/apps/sasquatch/build.gradle index 3d8f0ebbe8..0864db8ffc 100644 --- a/apps/sasquatch/build.gradle +++ b/apps/sasquatch/build.gradle @@ -21,8 +21,14 @@ android { } } +repositories { + maven { + url "http://dl.bintray.com/mobile-center/mobile-center" + } +} + dependencies { - def version = "0.7.0" + def version = "0.8.0" compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}" projectDependencyCompile project(':sdk:mobile-center-analytics') projectDependencyCompile project(':sdk:mobile-center-crashes') @@ -31,6 +37,7 @@ dependencies { jcenterDependencyCompile "com.microsoft.azure.mobile:mobile-center-analytics:${version}" jcenterDependencyCompile "com.microsoft.azure.mobile:mobile-center-crashes:${version}" jcenterDependencyCompile "com.microsoft.azure.mobile:mobile-center-distribute:${version}" + jcenterDependencyCompile "com.microsoft.azure.mobile:mobile-center-push:${version}" /* Force usage this version of support annotations to avoid conflict. */ androidTestCompile "com.android.support:support-annotations:${rootProject.ext.supportLibVersion}" diff --git a/apps/sasquatch/src/jcenterDependency/java/com/microsoft/azure/mobile/sasquatch/features/PushListenerHelper.java b/apps/sasquatch/src/jcenterDependency/java/com/microsoft/azure/mobile/sasquatch/features/PushListenerHelper.java deleted file mode 100644 index f6a49dae7c..0000000000 --- a/apps/sasquatch/src/jcenterDependency/java/com/microsoft/azure/mobile/sasquatch/features/PushListenerHelper.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.microsoft.azure.mobile.sasquatch.features; - -public class PushListenerHelper { - - public static void setup() { - - /* Not available in jCenter yet. See project build flavour class file. */ - } -} diff --git a/apps/sasquatch/src/main/java/com/microsoft/azure/mobile/sasquatch/activities/MainActivity.java b/apps/sasquatch/src/main/java/com/microsoft/azure/mobile/sasquatch/activities/MainActivity.java index 9f4fdf67ca..9dda72af28 100644 --- a/apps/sasquatch/src/main/java/com/microsoft/azure/mobile/sasquatch/activities/MainActivity.java +++ b/apps/sasquatch/src/main/java/com/microsoft/azure/mobile/sasquatch/activities/MainActivity.java @@ -1,11 +1,13 @@ package com.microsoft.azure.mobile.sasquatch.activities; +import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.StrictMode; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.VisibleForTesting; import android.support.test.espresso.idling.CountingIdlingResource; @@ -20,7 +22,6 @@ import android.widget.Toast; import com.microsoft.azure.mobile.MobileCenter; -import com.microsoft.azure.mobile.MobileCenterService; import com.microsoft.azure.mobile.ResultCallback; import com.microsoft.azure.mobile.analytics.Analytics; import com.microsoft.azure.mobile.analytics.AnalyticsPrivateHelper; @@ -32,15 +33,19 @@ import com.microsoft.azure.mobile.crashes.model.ErrorReport; import com.microsoft.azure.mobile.distribute.Distribute; import com.microsoft.azure.mobile.ingestion.models.LogWithProperties; +import com.microsoft.azure.mobile.push.Push; +import com.microsoft.azure.mobile.push.PushListener; +import com.microsoft.azure.mobile.push.PushNotification; import com.microsoft.azure.mobile.sasquatch.R; import com.microsoft.azure.mobile.sasquatch.SasquatchDistributeListener; -import com.microsoft.azure.mobile.sasquatch.features.PushListenerHelper; import com.microsoft.azure.mobile.sasquatch.features.TestFeatures; import com.microsoft.azure.mobile.sasquatch.features.TestFeaturesListAdapter; import com.microsoft.azure.mobile.utils.MobileCenterLog; import org.json.JSONObject; +import java.util.Map; + public class MainActivity extends AppCompatActivity { public static final String LOG_TAG = "MobileCenterSasquatch"; @@ -71,7 +76,7 @@ protected void onCreate(Bundle savedInstanceState) { AnalyticsPrivateHelper.setListener(getAnalyticsListener()); Crashes.setListener(getCrashesListener()); Distribute.setListener(new SasquatchDistributeListener()); - PushListenerHelper.setup(); + Push.setListener(getPushListener()); /* Set distribute urls. */ String installUrl = getString(R.string.install_url); @@ -83,33 +88,13 @@ protected void onCreate(Bundle savedInstanceState) { Distribute.setApiUrl(apiUrl); } - /* Get push module reference in project build flavour. */ - Class push = null; - try { - //noinspection unchecked - push = (Class) Class.forName("com.microsoft.azure.mobile.push.Push"); - } catch (Exception e) { - MobileCenterLog.warn(LOG_TAG, "Push class not yet available in this flavor."); - } - /* Enable Firebase analytics if we enabled the setting previously. */ - if (push != null && sSharedPreferences.getBoolean(FIREBASE_ENABLED_KEY, false)) { - try { - push.getMethod("enableFirebaseAnalytics", Context.class).invoke(null, this); - MobileCenterLog.info(LOG_TAG, "Enabled firebase analytics."); - } catch (Exception e) { - MobileCenterLog.error(LOG_TAG, "Failed to enable firebase analytics.", e); - } + if (sSharedPreferences.getBoolean(FIREBASE_ENABLED_KEY, false)) { + Push.enableFirebaseAnalytics(this); } /* Start Mobile center. */ - MobileCenter.start(getApplication(), sSharedPreferences.getString(APP_SECRET_KEY, getString(R.string.app_secret)), Analytics.class, Crashes.class, Distribute.class); - if (push != null) - try { - MobileCenter.start(push); - } catch (Exception e) { - MobileCenterLog.error(LOG_TAG, "Failed to start push.", e); - } + MobileCenter.start(getApplication(), sSharedPreferences.getString(APP_SECRET_KEY, getString(R.string.app_secret)), Analytics.class, Crashes.class, Distribute.class, Push.class); /* Print last crash. */ Log.i(LOG_TAG, "Crashes.hasCrashedInLastSession=" + Crashes.hasCrashedInLastSession()); @@ -196,9 +181,8 @@ public void onSendingFailed(ErrorReport report, Exception e) { } @Override + @SuppressWarnings("ThrowableResultOfMethodCallIgnored") public void onSendingSucceeded(ErrorReport report) { - - @SuppressWarnings("ThrowableResultOfMethodCallIgnored") String message = String.format("%s\nCrash ID: %s", getString(R.string.crash_sent_succeeded), report.getId()); if (report.getThrowable() != null) { message += String.format("\nThrowable: %s", report.getThrowable().toString()); @@ -255,4 +239,30 @@ public void onSendingSucceeded(com.microsoft.azure.mobile.ingestion.models.Log l } }; } + + @NonNull + private PushListener getPushListener() { + return new PushListener() { + + @Override + public void onPushNotificationReceived(Activity activity, PushNotification pushNotification) { + String title = pushNotification.getTitle(); + String message = pushNotification.getMessage(); + Map customData = pushNotification.getCustomData(); + MobileCenterLog.info(MainActivity.LOG_TAG, "Push received title=" + title + " message=" + message + " customData=" + customData + " activity=" + activity); + if (message != null) { + android.app.AlertDialog.Builder dialog = new android.app.AlertDialog.Builder(activity); + dialog.setTitle(title); + dialog.setMessage(message); + if (!customData.isEmpty()) { + dialog.setMessage(message + "\n" + customData); + } + dialog.setPositiveButton(android.R.string.ok, null); + dialog.show(); + } else { + Toast.makeText(activity, String.format(activity.getString(R.string.push_toast), customData), Toast.LENGTH_LONG).show(); + } + } + }; + } } diff --git a/apps/sasquatch/src/main/java/com/microsoft/azure/mobile/sasquatch/activities/SettingsActivity.java b/apps/sasquatch/src/main/java/com/microsoft/azure/mobile/sasquatch/activities/SettingsActivity.java index 71e98c7531..c63c632455 100644 --- a/apps/sasquatch/src/main/java/com/microsoft/azure/mobile/sasquatch/activities/SettingsActivity.java +++ b/apps/sasquatch/src/main/java/com/microsoft/azure/mobile/sasquatch/activities/SettingsActivity.java @@ -1,7 +1,6 @@ package com.microsoft.azure.mobile.sasquatch.activities; import android.app.AlertDialog; -import android.content.Context; import android.content.DialogInterface; import android.content.SharedPreferences; import android.os.Bundle; @@ -19,16 +18,15 @@ import com.google.firebase.analytics.FirebaseAnalytics; import com.microsoft.azure.mobile.MobileCenter; -import com.microsoft.azure.mobile.MobileCenterService; import com.microsoft.azure.mobile.analytics.Analytics; import com.microsoft.azure.mobile.analytics.AnalyticsPrivateHelper; import com.microsoft.azure.mobile.crashes.Crashes; import com.microsoft.azure.mobile.distribute.Distribute; +import com.microsoft.azure.mobile.push.Push; import com.microsoft.azure.mobile.sasquatch.R; import com.microsoft.azure.mobile.utils.PrefStorageConstants; import com.microsoft.azure.mobile.utils.storage.StorageHelper; -import java.lang.reflect.Method; import java.util.UUID; import static com.microsoft.azure.mobile.sasquatch.activities.MainActivity.APP_SECRET_KEY; @@ -93,11 +91,7 @@ public boolean isEnabled() { @Override public void setEnabled(boolean enabled) { - try { - Distribute.setEnabled(enabled); - } catch (Exception e) { - throw new RuntimeException(e); - } + Distribute.setEnabled(enabled); } @Override @@ -106,40 +100,25 @@ public boolean isEnabled() { } }); try { - - @SuppressWarnings("unchecked") - Class push = (Class) Class.forName("com.microsoft.azure.mobile.push.Push"); - final Method isEnabled = push.getMethod("isEnabled"); - final Method setEnabled = push.getMethod("setEnabled", boolean.class); initCheckBoxSetting(R.string.mobile_center_push_state_key, R.string.mobile_center_push_state_summary_enabled, R.string.mobile_center_push_state_summary_disabled, new HasEnabled() { @Override public void setEnabled(boolean enabled) { - try { - setEnabled.invoke(null, enabled); - } catch (Exception e) { - throw new RuntimeException(e); - } + Push.setEnabled(enabled); } @Override public boolean isEnabled() { - try { - return (boolean) isEnabled.invoke(null); - } catch (Exception e) { - throw new RuntimeException(e); - } + return Push.isEnabled(); } }); - - final Method enableFirebaseAnalytics = push.getMethod("enableFirebaseAnalytics", Context.class); initCheckBoxSetting(R.string.mobile_center_push_firebase_state_key, R.string.mobile_center_push_firebase_summary_enabled, R.string.mobile_center_push_firebase_summary_disabled, new HasEnabled() { @Override public void setEnabled(boolean enabled) { try { if (enabled) { - enableFirebaseAnalytics.invoke(null, getActivity()); + Push.enableFirebaseAnalytics(getActivity()); } else { FirebaseAnalytics.getInstance(getActivity()).setAnalyticsCollectionEnabled(false); } diff --git a/apps/sasquatch/src/projectDependency/java/com/microsoft/azure/mobile/sasquatch/features/PushListenerHelper.java b/apps/sasquatch/src/projectDependency/java/com/microsoft/azure/mobile/sasquatch/features/PushListenerHelper.java deleted file mode 100644 index 01e10be5a1..0000000000 --- a/apps/sasquatch/src/projectDependency/java/com/microsoft/azure/mobile/sasquatch/features/PushListenerHelper.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.microsoft.azure.mobile.sasquatch.features; - -import android.app.Activity; -import android.app.AlertDialog; -import android.widget.Toast; - -import com.microsoft.azure.mobile.push.Push; -import com.microsoft.azure.mobile.push.PushListener; -import com.microsoft.azure.mobile.push.PushNotification; -import com.microsoft.azure.mobile.sasquatch.R; -import com.microsoft.azure.mobile.sasquatch.activities.MainActivity; -import com.microsoft.azure.mobile.utils.MobileCenterLog; - -import java.util.Map; - -public class PushListenerHelper { - - public static void setup() { - Push.setListener(new PushListener() { - - @Override - public void onPushNotificationReceived(Activity activity, PushNotification pushNotification) { - String title = pushNotification.getTitle(); - String message = pushNotification.getMessage(); - Map customData = pushNotification.getCustomData(); - MobileCenterLog.info(MainActivity.LOG_TAG, "Push received title=" + title + " message=" + message + " customData=" + customData + " activity=" + activity); - if (message != null) { - AlertDialog.Builder dialog = new AlertDialog.Builder(activity); - dialog.setTitle(title); - dialog.setMessage(message); - if (!customData.isEmpty()) { - dialog.setMessage(message + "\n" + customData); - } - dialog.setPositiveButton(android.R.string.ok, null); - dialog.show(); - } else { - Toast.makeText(activity, String.format(activity.getString(R.string.push_toast), customData), Toast.LENGTH_LONG).show(); - } - } - }); - } -} diff --git a/versions.gradle b/versions.gradle index f7a0590bf7..bb0c17538b 100644 --- a/versions.gradle +++ b/versions.gradle @@ -1,7 +1,7 @@ // Version constants ext { - versionCode = 17 + versionCode = 18 versionName = '0.8.0' minSdkVersion = 15 targetSdkVersion = 25 From a55bdb117920fade4e00a92fbe68522da9d05527 Mon Sep 17 00:00:00 2001 From: Guillaume Perrot Date: Thu, 4 May 2017 14:11:04 -0700 Subject: [PATCH 2/2] Remove "private" repos --- apps/sasquatch/build.gradle | 6 ------ 1 file changed, 6 deletions(-) diff --git a/apps/sasquatch/build.gradle b/apps/sasquatch/build.gradle index 0864db8ffc..285d146bc6 100644 --- a/apps/sasquatch/build.gradle +++ b/apps/sasquatch/build.gradle @@ -21,12 +21,6 @@ android { } } -repositories { - maven { - url "http://dl.bintray.com/mobile-center/mobile-center" - } -} - dependencies { def version = "0.8.0" compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"