diff --git a/.gitignore b/.gitignore index 34b89654..cb70d220 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,5 @@ local.properties proguard/ # Log Files *.log +# IntelliJ files +*.iml diff --git a/Pushjet-Android.iml b/Pushjet-Android.iml deleted file mode 100644 index b3109a9e..00000000 --- a/Pushjet-Android.iml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/app.iml b/app/app.iml deleted file mode 100644 index 29287d18..00000000 --- a/app/app.iml +++ /dev/null @@ -1,163 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index c81209e3..8a03f8fd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -31,15 +31,14 @@ repositories { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.google.android.gms:play-services:8.4.0' compile 'com.google.zxing:core:2.2' + compile 'com.google.firebase:firebase-core:9.8.0' + compile 'com.google.firebase:firebase-iid:9.8.0' + compile 'com.google.firebase:firebase-messaging:9.8.0' compile 'com.journeyapps:zxing-android-embedded:2.3.0@aar' compile 'com.journeyapps:zxing-android-integration:2.3.0@aar' compile 'com.android.support:support-v4:24.0.0-alpha2' compile 'com.android.support:appcompat-v7:24.0.0-alpha2' - compile 'com.google.android.gms:play-services-ads:8.4.0' - compile 'com.google.android.gms:play-services-auth:8.4.0' - compile 'com.google.android.gms:play-services-gcm:8.4.0' } allprojects { @@ -49,3 +48,5 @@ allprojects { } } } + +apply plugin: 'com.google.gms.google-services' \ No newline at end of file diff --git a/app/google-services.json b/app/google-services.json new file mode 100644 index 00000000..51e0340b --- /dev/null +++ b/app/google-services.json @@ -0,0 +1,42 @@ +{ + "project_info": { + "project_number": "", + "firebase_url": "", + "project_id": "", + "storage_bucket": "" + }, + "client": [ + { + "client_info": { + "mobilesdk_app_id": "", + "android_client_info": { + "package_name": "io.Pushjet.api" + } + }, + "oauth_client": [ + { + "client_id": "", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "" + } + ], + "services": { + "analytics_service": { + "status": 1 + }, + "appinvite_service": { + "status": 1, + "other_platform_oauth_client": [] + }, + "ads_service": { + "status": 2 + } + } + } + ], + "configuration_version": "1" +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2ac3e6c6..7b5ae60e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,14 +4,9 @@ - - - - - - + - + + + - + + + - + - + + + + + keySet = intent.getExtras().keySet(); + Boolean force = false; + + if (keySet.contains(PROPERTY_UNREGISTER)) { + forgetRegistration(); + force = true; + } + + if (keySet.contains(PROPERTY_FCM_TOKEN)) { + String regId = String.valueOf(intent.getStringExtra(PROPERTY_FCM_TOKEN)); + storeToken(regId); } - return true; + + registerInBackground(force); + + return START_STICKY; + } + + @Nullable + @Override + public IBinder onBind(Intent intent) { + return null; + } + + + private void storeToken(String token) { + final SharedPreferences prefs = getGcmPreferences(mContext); + SharedPreferences.Editor editor = prefs.edit(); + + editor.putString(PROPERTY_FCM_TOKEN, token); + editor.apply(); } private void storeRegistrationId(String regId) { @@ -72,6 +89,10 @@ public String getRegistrationId() { return registrationId; } + private String getToken() { + return getGcmPreferences(mContext).getString(PROPERTY_FCM_TOKEN, ""); + } + public int getAppVersion() { try { PackageInfo packageInfo = mContext.getPackageManager() @@ -83,17 +104,7 @@ public int getAppVersion() { } private SharedPreferences getGcmPreferences(Context context) { - return context.getSharedPreferences(GCMRegistrar.class.getSimpleName(), Context.MODE_PRIVATE); - } - - public AsyncRegistrar registerInBackground() { - return this.registerInBackground(false); - } - - public AsyncRegistrar registerInBackground(boolean force) { - AsyncRegistrar task = new AsyncRegistrar(); - task.execute(force); - return task; + return context.getSharedPreferences(PushjetFirebaseInstanceIDService.class.getSimpleName(), Context.MODE_PRIVATE); } public boolean shouldRegister() { @@ -103,12 +114,21 @@ public boolean shouldRegister() { public void forgetRegistration() { final SharedPreferences prefs = getGcmPreferences(mContext); SharedPreferences.Editor editor = prefs.edit(); - - editor.putString(PROPERTY_REG_ID, ""); - editor.putInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE); + editor.remove(PROPERTY_REG_ID); + editor.remove(PROPERTY_APP_VERSION); editor.commit(); } + public AsyncRegistrar registerInBackground() { + return this.registerInBackground(false); + } + + public AsyncRegistrar registerInBackground(boolean force) { + AsyncRegistrar task = new AsyncRegistrar(); + task.execute(force); + return task; + } + private static boolean asyncAlreadyRunning = false; public class AsyncRegistrar extends AsyncTask { @@ -131,10 +151,7 @@ protected Void doInBackground(Boolean... params) { Looper.prepare(); try { - if (gcm == null) { - gcm = GoogleCloudMessaging.getInstance(mContext); - } - String regid = gcm.register(senderId); + String regid = getToken(); Map data = new HashMap(); data.put("regId", regid); @@ -172,3 +189,5 @@ protected Void doInBackground(Boolean... params) { } } } + + diff --git a/app/src/main/java/io/Pushjet/api/GcmBroadcastReceiver.java b/app/src/main/java/io/Pushjet/api/GcmBroadcastReceiver.java deleted file mode 100644 index 253f3015..00000000 --- a/app/src/main/java/io/Pushjet/api/GcmBroadcastReceiver.java +++ /dev/null @@ -1,19 +0,0 @@ -package io.Pushjet.api; - -import android.app.Activity; -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.support.v4.content.WakefulBroadcastReceiver; - -public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { - - @Override - public void onReceive(Context context, Intent intent) { - // Explicitly specify that GcmIntentService will handle the intent. - ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName()); - // Start the service, keeping the device awake while it is launching. - startWakefulService(context, (intent.setComponent(comp))); - setResultCode(Activity.RESULT_OK); - } -} \ No newline at end of file diff --git a/app/src/main/java/io/Pushjet/api/PushListActivity.java b/app/src/main/java/io/Pushjet/api/PushListActivity.java index 5d9b14da..1ba1aad0 100644 --- a/app/src/main/java/io/Pushjet/api/PushListActivity.java +++ b/app/src/main/java/io/Pushjet/api/PushListActivity.java @@ -17,7 +17,6 @@ import android.widget.Toast; import io.Pushjet.api.Async.FirstLaunchAsync; -import io.Pushjet.api.Async.GCMRegistrar; import io.Pushjet.api.Async.ReceivePushAsync; import io.Pushjet.api.Async.ReceivePushCallback; import io.Pushjet.api.PushjetApi.PushjetApi; @@ -83,16 +82,6 @@ public boolean onItemLongClick(AdapterView parent, View v, int position, long } }); - - GCMRegistrar mGCMRegistrar = new GCMRegistrar(getApplicationContext()); - if (firstLaunch || mGCMRegistrar.shouldRegister()) { - if (mGCMRegistrar.checkPlayServices(this)) { - mGCMRegistrar.registerInBackground(firstLaunch); - } else { - finish(); - } - } - receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { diff --git a/app/src/main/java/io/Pushjet/api/GcmIntentService.java b/app/src/main/java/io/Pushjet/api/PushjetFcmListenerService.java similarity index 61% rename from app/src/main/java/io/Pushjet/api/GcmIntentService.java rename to app/src/main/java/io/Pushjet/api/PushjetFcmListenerService.java index cfc99f95..6f3e9a2c 100644 --- a/app/src/main/java/io/Pushjet/api/GcmIntentService.java +++ b/app/src/main/java/io/Pushjet/api/PushjetFcmListenerService.java @@ -2,7 +2,6 @@ import android.annotation.TargetApi; -import android.app.IntentService; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; @@ -12,11 +11,11 @@ import android.graphics.Bitmap; import android.net.Uri; import android.os.Build; -import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.util.Log; -import com.google.android.gms.gcm.GoogleCloudMessaging; +import com.google.firebase.messaging.FirebaseMessagingService; +import com.google.firebase.messaging.RemoteMessage; import io.Pushjet.api.PushjetApi.PushjetMessage; import io.Pushjet.api.PushjetApi.PushjetService; @@ -26,48 +25,40 @@ import java.io.IOException; import java.util.Date; +import java.util.Map; -public class GcmIntentService extends IntentService { +public class PushjetFcmListenerService extends FirebaseMessagingService { private static int NOTIFICATION_ID = 0; - - public GcmIntentService() { - super("GcmIntentService"); - } + private static final String TAG = "PushjetGcmListeners"; @Override - protected void onHandleIntent(Intent intent) { - Bundle extras = intent.getExtras(); - GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); - String messageType = gcm.getMessageType(intent); - - if (!extras.isEmpty() && GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { - try { - JSONObject AzMsg = new JSONObject(extras.getString("message")); - JSONObject AzServ = AzMsg.getJSONObject("service"); - PushjetService srv = new PushjetService( - AzServ.getString("public"), - AzServ.getString("name"), - new Date((long) AzServ.getInt("created") * 1000) - ); - srv.setIcon(AzServ.getString("icon")); - - PushjetMessage msg = new PushjetMessage( - srv, - AzMsg.getString("message"), - AzMsg.getString("title"), - AzMsg.getInt("timestamp") - ); - msg.setLevel(AzMsg.getInt("level")); - msg.setLink(AzMsg.getString("link")); - DatabaseHandler db = new DatabaseHandler(this); - db.addMessage(msg); - sendNotification(msg); - } catch (JSONException ignore) { - Log.e("PushjetJson", ignore.getMessage()); - } + public void onMessageReceived(RemoteMessage message) { + String from = message.getFrom(); + Map data = message.getData(); + try { + JSONObject AzMsg = new JSONObject((String)data.get("message")); + JSONObject AzServ = AzMsg.getJSONObject("service"); + PushjetService srv = new PushjetService( + AzServ.getString("public"), + AzServ.getString("name"), + new Date((long) AzServ.getInt("created") * 1000) + ); + srv.setIcon(AzServ.getString("icon")); + + PushjetMessage msg = new PushjetMessage( + srv, + AzMsg.getString("message"), + AzMsg.getString("title"), + AzMsg.getInt("timestamp") + ); + msg.setLevel(AzMsg.getInt("level")); + msg.setLink(AzMsg.getString("link")); + DatabaseHandler db = new DatabaseHandler(this); + db.addMessage(msg); + sendNotification(msg); + } catch (JSONException ignore) { + Log.e("PushjetJson", ignore.getMessage()); } - GcmBroadcastReceiver.completeWakefulIntent(intent); - sendBroadcast(new Intent("PushjetMessageRefresh")); } private void sendNotification(PushjetMessage msg) { @@ -124,4 +115,3 @@ private void setPriority(NotificationCompat.Builder mBuilder, PushjetMessage msg mBuilder.setPriority(priority); } } - diff --git a/app/src/main/java/io/Pushjet/api/PushjetFirebaseInstanceIDService.java b/app/src/main/java/io/Pushjet/api/PushjetFirebaseInstanceIDService.java new file mode 100644 index 00000000..6f367e79 --- /dev/null +++ b/app/src/main/java/io/Pushjet/api/PushjetFirebaseInstanceIDService.java @@ -0,0 +1,26 @@ +package io.Pushjet.api; + + +import android.content.Intent; +import android.util.Log; + +import com.google.firebase.iid.FirebaseInstanceId; +import com.google.firebase.iid.FirebaseInstanceIdService; + +import io.Pushjet.api.Async.PushjetRegistrationService; + +public class PushjetFirebaseInstanceIDService extends FirebaseInstanceIdService { + protected final String TAG = "PushjetFCMIIDService"; + + @Override + public void onTokenRefresh() { + // Get updated InstanceID token. + String refreshedToken = FirebaseInstanceId.getInstance().getToken(); + Log.d(TAG, "FCM Token refreshed."); + + // call PushjetRegistrationService to update the token + Intent intent = new Intent(this, PushjetRegistrationService.class); + intent.putExtra(PushjetRegistrationService.PROPERTY_FCM_TOKEN, refreshedToken); + startService(intent); + } +} diff --git a/app/src/main/java/io/Pushjet/api/SettingsActivity.java b/app/src/main/java/io/Pushjet/api/SettingsActivity.java index 15f02bc4..e8d686f7 100644 --- a/app/src/main/java/io/Pushjet/api/SettingsActivity.java +++ b/app/src/main/java/io/Pushjet/api/SettingsActivity.java @@ -4,6 +4,7 @@ import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; +import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.os.Build; @@ -15,7 +16,7 @@ import android.preference.PreferenceFragment; import android.preference.PreferenceManager; -import io.Pushjet.api.Async.GCMRegistrar; +import io.Pushjet.api.Async.PushjetRegistrationService; import io.Pushjet.api.Async.RefreshServiceAsync; import io.Pushjet.api.PushjetApi.PushjetApi; @@ -71,12 +72,12 @@ public void onClick(DialogInterface dialog, int which) { builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - GCMRegistrar gcm = new GCMRegistrar(context); - - gcm.forgetRegistration(); db.truncateMessages(); db.truncateServices(); - gcm.registerInBackground(true); + + Intent intent = new Intent(context, PushjetRegistrationService.class); + intent.putExtra(PushjetRegistrationService.PROPERTY_UNREGISTER, ""); + context.startService(intent); PushjetApi api = new PushjetApi(context, getRegisterUrl(context)); new RefreshServiceAsync(api, db).execute(); diff --git a/build.gradle b/build.gradle index 3a9dee85..96c90f5a 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,8 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:1.5.0' + classpath 'com.android.tools.build:gradle:2.2.2' + classpath 'com.google.gms:google-services:3.0.0' // Lowest version for useLibrary is 1.3.0 // Android Studio will notify you about the latest stable version // See all versions: http://jcenter.bintray.com/com/android/tools/build/gradle/ diff --git a/circle.yml b/circle.yml index 3aa123c1..19ff33bd 100644 --- a/circle.yml +++ b/circle.yml @@ -1,3 +1,8 @@ +dependencies: + pre: + - echo y | android update sdk -u -a -t tool + - echo y | android update sdk --no-ui --all --filter "android-23,build-tools-23.0.3" + test: override: - ./gradlew clean assembleDebug --recompile-scripts --refresh-dependencies --parallel --stacktrace @@ -5,3 +10,7 @@ test: general: artifacts: - "app/build/outputs/apk/app-debug.apk" # Installable APK that users can install live on the bleeding edge + +machine: + java: + version: 'oraclejdk8' diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 886e8203..aaa47c70 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Mar 25 20:37:33 CET 2015 +#Sat Nov 12 18:52:06 CET 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip diff --git a/pushjet-android.iml b/pushjet-android.iml deleted file mode 100644 index 78ea65c1..00000000 --- a/pushjet-android.iml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file