Skip to content

Commit d744815

Browse files
authored
Merge pull request #428 from Microsoft/merge/master2develop
Merge/master2develop
2 parents 5f1c9bb + 7483e4c commit d744815

File tree

5 files changed

+45
-106
lines changed

5 files changed

+45
-106
lines changed

apps/sasquatch/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ android {
2222
}
2323

2424
dependencies {
25-
def version = "0.7.0"
25+
def version = "0.8.0"
2626
compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
2727
projectDependencyCompile project(':sdk:mobile-center-analytics')
2828
projectDependencyCompile project(':sdk:mobile-center-crashes')
@@ -31,6 +31,7 @@ dependencies {
3131
jcenterDependencyCompile "com.microsoft.azure.mobile:mobile-center-analytics:${version}"
3232
jcenterDependencyCompile "com.microsoft.azure.mobile:mobile-center-crashes:${version}"
3333
jcenterDependencyCompile "com.microsoft.azure.mobile:mobile-center-distribute:${version}"
34+
jcenterDependencyCompile "com.microsoft.azure.mobile:mobile-center-push:${version}"
3435

3536
/* Force usage this version of support annotations to avoid conflict. */
3637
androidTestCompile "com.android.support:support-annotations:${rootProject.ext.supportLibVersion}"

apps/sasquatch/src/jcenterDependency/java/com/microsoft/azure/mobile/sasquatch/features/PushListenerHelper.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

apps/sasquatch/src/main/java/com/microsoft/azure/mobile/sasquatch/activities/MainActivity.java

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.microsoft.azure.mobile.sasquatch.activities;
22

3+
import android.app.Activity;
34
import android.content.Context;
45
import android.content.DialogInterface;
56
import android.content.Intent;
67
import android.content.SharedPreferences;
78
import android.os.Bundle;
89
import android.os.StrictMode;
10+
import android.support.annotation.NonNull;
911
import android.support.annotation.Nullable;
1012
import android.support.annotation.VisibleForTesting;
1113
import android.support.test.espresso.idling.CountingIdlingResource;
@@ -20,7 +22,6 @@
2022
import android.widget.Toast;
2123

2224
import com.microsoft.azure.mobile.MobileCenter;
23-
import com.microsoft.azure.mobile.MobileCenterService;
2425
import com.microsoft.azure.mobile.ResultCallback;
2526
import com.microsoft.azure.mobile.analytics.Analytics;
2627
import com.microsoft.azure.mobile.analytics.AnalyticsPrivateHelper;
@@ -32,15 +33,19 @@
3233
import com.microsoft.azure.mobile.crashes.model.ErrorReport;
3334
import com.microsoft.azure.mobile.distribute.Distribute;
3435
import com.microsoft.azure.mobile.ingestion.models.LogWithProperties;
36+
import com.microsoft.azure.mobile.push.Push;
37+
import com.microsoft.azure.mobile.push.PushListener;
38+
import com.microsoft.azure.mobile.push.PushNotification;
3539
import com.microsoft.azure.mobile.sasquatch.R;
3640
import com.microsoft.azure.mobile.sasquatch.SasquatchDistributeListener;
37-
import com.microsoft.azure.mobile.sasquatch.features.PushListenerHelper;
3841
import com.microsoft.azure.mobile.sasquatch.features.TestFeatures;
3942
import com.microsoft.azure.mobile.sasquatch.features.TestFeaturesListAdapter;
4043
import com.microsoft.azure.mobile.utils.MobileCenterLog;
4144

4245
import org.json.JSONObject;
4346

47+
import java.util.Map;
48+
4449
public class MainActivity extends AppCompatActivity {
4550

4651
public static final String LOG_TAG = "MobileCenterSasquatch";
@@ -71,7 +76,7 @@ protected void onCreate(Bundle savedInstanceState) {
7176
AnalyticsPrivateHelper.setListener(getAnalyticsListener());
7277
Crashes.setListener(getCrashesListener());
7378
Distribute.setListener(new SasquatchDistributeListener());
74-
PushListenerHelper.setup();
79+
Push.setListener(getPushListener());
7580

7681
/* Set distribute urls. */
7782
String installUrl = getString(R.string.install_url);
@@ -83,33 +88,13 @@ protected void onCreate(Bundle savedInstanceState) {
8388
Distribute.setApiUrl(apiUrl);
8489
}
8590

86-
/* Get push module reference in project build flavour. */
87-
Class<? extends MobileCenterService> push = null;
88-
try {
89-
//noinspection unchecked
90-
push = (Class<? extends MobileCenterService>) Class.forName("com.microsoft.azure.mobile.push.Push");
91-
} catch (Exception e) {
92-
MobileCenterLog.warn(LOG_TAG, "Push class not yet available in this flavor.");
93-
}
94-
9591
/* Enable Firebase analytics if we enabled the setting previously. */
96-
if (push != null && sSharedPreferences.getBoolean(FIREBASE_ENABLED_KEY, false)) {
97-
try {
98-
push.getMethod("enableFirebaseAnalytics", Context.class).invoke(null, this);
99-
MobileCenterLog.info(LOG_TAG, "Enabled firebase analytics.");
100-
} catch (Exception e) {
101-
MobileCenterLog.error(LOG_TAG, "Failed to enable firebase analytics.", e);
102-
}
92+
if (sSharedPreferences.getBoolean(FIREBASE_ENABLED_KEY, false)) {
93+
Push.enableFirebaseAnalytics(this);
10394
}
10495

10596
/* Start Mobile center. */
106-
MobileCenter.start(getApplication(), sSharedPreferences.getString(APP_SECRET_KEY, getString(R.string.app_secret)), Analytics.class, Crashes.class, Distribute.class);
107-
if (push != null)
108-
try {
109-
MobileCenter.start(push);
110-
} catch (Exception e) {
111-
MobileCenterLog.error(LOG_TAG, "Failed to start push.", e);
112-
}
97+
MobileCenter.start(getApplication(), sSharedPreferences.getString(APP_SECRET_KEY, getString(R.string.app_secret)), Analytics.class, Crashes.class, Distribute.class, Push.class);
11398

11499
/* Print last crash. */
115100
Log.i(LOG_TAG, "Crashes.hasCrashedInLastSession=" + Crashes.hasCrashedInLastSession());
@@ -196,9 +181,8 @@ public void onSendingFailed(ErrorReport report, Exception e) {
196181
}
197182

198183
@Override
184+
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
199185
public void onSendingSucceeded(ErrorReport report) {
200-
201-
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
202186
String message = String.format("%s\nCrash ID: %s", getString(R.string.crash_sent_succeeded), report.getId());
203187
if (report.getThrowable() != null) {
204188
message += String.format("\nThrowable: %s", report.getThrowable().toString());
@@ -255,4 +239,30 @@ public void onSendingSucceeded(com.microsoft.azure.mobile.ingestion.models.Log l
255239
}
256240
};
257241
}
242+
243+
@NonNull
244+
private PushListener getPushListener() {
245+
return new PushListener() {
246+
247+
@Override
248+
public void onPushNotificationReceived(Activity activity, PushNotification pushNotification) {
249+
String title = pushNotification.getTitle();
250+
String message = pushNotification.getMessage();
251+
Map<String, String> customData = pushNotification.getCustomData();
252+
MobileCenterLog.info(MainActivity.LOG_TAG, "Push received title=" + title + " message=" + message + " customData=" + customData + " activity=" + activity);
253+
if (message != null) {
254+
android.app.AlertDialog.Builder dialog = new android.app.AlertDialog.Builder(activity);
255+
dialog.setTitle(title);
256+
dialog.setMessage(message);
257+
if (!customData.isEmpty()) {
258+
dialog.setMessage(message + "\n" + customData);
259+
}
260+
dialog.setPositiveButton(android.R.string.ok, null);
261+
dialog.show();
262+
} else {
263+
Toast.makeText(activity, String.format(activity.getString(R.string.push_toast), customData), Toast.LENGTH_LONG).show();
264+
}
265+
}
266+
};
267+
}
258268
}

apps/sasquatch/src/main/java/com/microsoft/azure/mobile/sasquatch/activities/SettingsActivity.java

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.microsoft.azure.mobile.sasquatch.activities;
22

33
import android.app.AlertDialog;
4-
import android.content.Context;
54
import android.content.DialogInterface;
65
import android.content.SharedPreferences;
76
import android.os.Bundle;
@@ -19,16 +18,15 @@
1918

2019
import com.google.firebase.analytics.FirebaseAnalytics;
2120
import com.microsoft.azure.mobile.MobileCenter;
22-
import com.microsoft.azure.mobile.MobileCenterService;
2321
import com.microsoft.azure.mobile.analytics.Analytics;
2422
import com.microsoft.azure.mobile.analytics.AnalyticsPrivateHelper;
2523
import com.microsoft.azure.mobile.crashes.Crashes;
2624
import com.microsoft.azure.mobile.distribute.Distribute;
25+
import com.microsoft.azure.mobile.push.Push;
2726
import com.microsoft.azure.mobile.sasquatch.R;
2827
import com.microsoft.azure.mobile.utils.PrefStorageConstants;
2928
import com.microsoft.azure.mobile.utils.storage.StorageHelper;
3029

31-
import java.lang.reflect.Method;
3230
import java.util.UUID;
3331

3432
import static com.microsoft.azure.mobile.sasquatch.activities.MainActivity.APP_SECRET_KEY;
@@ -93,11 +91,7 @@ public boolean isEnabled() {
9391

9492
@Override
9593
public void setEnabled(boolean enabled) {
96-
try {
97-
Distribute.setEnabled(enabled);
98-
} catch (Exception e) {
99-
throw new RuntimeException(e);
100-
}
94+
Distribute.setEnabled(enabled);
10195
}
10296

10397
@Override
@@ -106,40 +100,25 @@ public boolean isEnabled() {
106100
}
107101
});
108102
try {
109-
110-
@SuppressWarnings("unchecked")
111-
Class<? extends MobileCenterService> push = (Class<? extends MobileCenterService>) Class.forName("com.microsoft.azure.mobile.push.Push");
112-
final Method isEnabled = push.getMethod("isEnabled");
113-
final Method setEnabled = push.getMethod("setEnabled", boolean.class);
114103
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() {
115104

116105
@Override
117106
public void setEnabled(boolean enabled) {
118-
try {
119-
setEnabled.invoke(null, enabled);
120-
} catch (Exception e) {
121-
throw new RuntimeException(e);
122-
}
107+
Push.setEnabled(enabled);
123108
}
124109

125110
@Override
126111
public boolean isEnabled() {
127-
try {
128-
return (boolean) isEnabled.invoke(null);
129-
} catch (Exception e) {
130-
throw new RuntimeException(e);
131-
}
112+
return Push.isEnabled();
132113
}
133114
});
134-
135-
final Method enableFirebaseAnalytics = push.getMethod("enableFirebaseAnalytics", Context.class);
136115
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() {
137116

138117
@Override
139118
public void setEnabled(boolean enabled) {
140119
try {
141120
if (enabled) {
142-
enableFirebaseAnalytics.invoke(null, getActivity());
121+
Push.enableFirebaseAnalytics(getActivity());
143122
} else {
144123
FirebaseAnalytics.getInstance(getActivity()).setAnalyticsCollectionEnabled(false);
145124
}

apps/sasquatch/src/projectDependency/java/com/microsoft/azure/mobile/sasquatch/features/PushListenerHelper.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)