Skip to content

Commit

Permalink
Merge pull request #425 from Microsoft/release/demo_v0.8.0
Browse files Browse the repository at this point in the history
Add Push to jCenter build flavour and use 0.8.0 SDK
  • Loading branch information
guperrot authored May 4, 2017
2 parents 673bd6c + a55bdb1 commit 098f421
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 107 deletions.
3 changes: 2 additions & 1 deletion apps/sasquatch/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ android {
}

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')
Expand All @@ -31,6 +31,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}"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -83,33 +88,13 @@ protected void onCreate(Bundle savedInstanceState) {
Distribute.setApiUrl(apiUrl);
}

/* Get push module reference in project build flavour. */
Class<? extends MobileCenterService> push = null;
try {
//noinspection unchecked
push = (Class<? extends MobileCenterService>) 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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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<String, String> 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();
}
}
};
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -106,40 +100,25 @@ public boolean isEnabled() {
}
});
try {

@SuppressWarnings("unchecked")
Class<? extends MobileCenterService> push = (Class<? extends MobileCenterService>) 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);
}
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion versions.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Version constants

ext {
versionCode = 17
versionCode = 18
versionName = '0.8.0'
minSdkVersion = 15
targetSdkVersion = 25
Expand Down

0 comments on commit 098f421

Please sign in to comment.