فهرست منبع

Basics of push

Mario Danic 8 سال پیش
والد
کامیت
dfc95e6edd

+ 40 - 9
src/main/java/com/owncloud/android/authentication/AccountAuthenticator.java

@@ -21,16 +21,19 @@
 
 package com.owncloud.android.authentication;
 
-import com.owncloud.android.MainApp;
-import com.owncloud.android.R;
-
-import android.accounts.*;
+import android.accounts.AbstractAccountAuthenticator;
+import android.accounts.Account;
+import android.accounts.AccountAuthenticatorResponse;
+import android.accounts.AccountManager;
+import android.accounts.NetworkErrorException;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.os.Handler;
 import android.widget.Toast;
 
+import com.owncloud.android.MainApp;
+import com.owncloud.android.R;
 import com.owncloud.android.lib.common.accounts.AccountTypeUtils;
 import com.owncloud.android.lib.common.utils.Log_OC;
 
@@ -90,8 +93,15 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator {
                         + e.getMessage(), e);
                 return e.getFailureBundle();
             }
-            
-            final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
+
+            Intent intent;
+            if (!mContext.getResources().getBoolean(R.bool.push_enabled) &&
+                    !mContext.getResources().getBoolean(R.bool.analytics_enabled)) {
+                intent = new Intent(mContext, AuthenticatorActivity.class);
+            } else {
+                intent = new Intent(mContext, ModifiedAuthenticatorActivity.class);
+            }
+
             intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
             intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
             intent.putExtra(KEY_REQUIRED_FEATURES, requiredFeatures);
@@ -134,7 +144,15 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator {
             Log_OC.e(TAG, "Failed to validate account type " + account.type + ": " + e.getMessage(), e);
             return e.getFailureBundle();
         }
-        Intent intent = new Intent(mContext, AuthenticatorActivity.class);
+
+        Intent intent;
+        if (!mContext.getResources().getBoolean(R.bool.push_enabled)
+                && !mContext.getResources().getBoolean(R.bool.analytics_enabled)) {
+            intent = new Intent(mContext, AuthenticatorActivity.class);
+        } else {
+            intent = new Intent(mContext, ModifiedAuthenticatorActivity.class);
+        }
+
         intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,
                 response);
         intent.putExtra(KEY_ACCOUNT, account);
@@ -185,7 +203,13 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator {
         }
         
         /// if not stored, return Intent to access the AuthenticatorActivity and UPDATE the token for the account
-        final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
+        Intent intent;
+        if (!mContext.getResources().getBoolean(R.bool.push_enabled)
+                && !mContext.getResources().getBoolean(R.bool.analytics_enabled)) {
+            intent = new Intent(mContext, AuthenticatorActivity.class);
+        } else {
+            intent = new Intent(mContext, ModifiedAuthenticatorActivity.class);
+        }
         intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
         intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
         intent.putExtra(KEY_LOGIN_OPTIONS, options);
@@ -215,7 +239,14 @@ public class AccountAuthenticator extends AbstractAccountAuthenticator {
     public Bundle updateCredentials(AccountAuthenticatorResponse response,
             Account account, String authTokenType, Bundle options)
             throws NetworkErrorException {
-        final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
+
+        Intent intent;
+        if (!mContext.getResources().getBoolean(R.bool.push_enabled) &&
+                !mContext.getResources().getBoolean(R.bool.analytics_enabled)) {
+            intent = new Intent(mContext, AuthenticatorActivity.class);
+        } else {
+            intent = new Intent(mContext, ModifiedAuthenticatorActivity.class);
+        }
         intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
         intent.putExtra(KEY_ACCOUNT, account);
         intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);

+ 10 - 2
src/main/java/com/owncloud/android/files/services/FileDownloader.java

@@ -41,6 +41,7 @@ import android.util.Pair;
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.authentication.AuthenticatorActivity;
+import com.owncloud.android.authentication.ModifiedAuthenticatorActivity;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.lib.common.OwnCloudAccount;
@@ -51,10 +52,10 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.resources.files.FileUtils;
-import com.owncloud.android.ui.notifications.NotificationUtils;
 import com.owncloud.android.operations.DownloadFileOperation;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.activity.FileDisplayActivity;
+import com.owncloud.android.ui.notifications.NotificationUtils;
 import com.owncloud.android.ui.preview.PreviewImageActivity;
 import com.owncloud.android.ui.preview.PreviewImageFragment;
 import com.owncloud.android.utils.ErrorMessageAdapter;
@@ -579,7 +580,14 @@ public class FileDownloader extends Service
             if (needsToUpdateCredentials) {
 
                 // let the user update credentials with one click
-                Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
+                Intent updateAccountCredentials;
+                if (!getResources().getBoolean(R.bool.push_enabled)
+                        && !getResources().getBoolean(R.bool.analytics_enabled)) {
+                    updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
+                } else {
+                    updateAccountCredentials = new Intent(this, ModifiedAuthenticatorActivity.class);
+                }
+
                 updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT,
                         download.getAccount());
                 updateAccountCredentials.putExtra(

+ 9 - 1
src/main/java/com/owncloud/android/files/services/FileUploader.java

@@ -48,6 +48,7 @@ import android.util.Pair;
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.authentication.AuthenticatorActivity;
+import com.owncloud.android.authentication.ModifiedAuthenticatorActivity;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
@@ -1080,7 +1081,14 @@ public class FileUploader extends Service
 
             if (needsToUpdateCredentials) {
                 // let the user update credentials with one click
-                Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
+                // let the user update credentials with one click
+                Intent updateAccountCredentials;
+                if (!getResources().getBoolean(R.bool.push_enabled)
+                        && !getResources().getBoolean(R.bool.analytics_enabled)) {
+                    updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
+                } else {
+                    updateAccountCredentials = new Intent(this, ModifiedAuthenticatorActivity.class);
+                }
                 updateAccountCredentials.putExtra(
                         AuthenticatorActivity.EXTRA_ACCOUNT, upload.getAccount()
                 );

+ 10 - 1
src/main/java/com/owncloud/android/syncadapter/FileSyncAdapter.java

@@ -37,6 +37,7 @@ import android.support.v4.app.NotificationCompat;
 
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AuthenticatorActivity;
+import com.owncloud.android.authentication.ModifiedAuthenticatorActivity;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
@@ -399,7 +400,15 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
         );
         if (needsToUpdateCredentials) {
             // let the user update credentials with one click
-            Intent updateAccountCredentials = new Intent(getContext(), AuthenticatorActivity.class);
+            // let the user update credentials with one click
+            Intent updateAccountCredentials;
+            if (!getContext().getResources().getBoolean(R.bool.push_enabled)
+                    && !getContext().getResources().getBoolean(R.bool.analytics_enabled)) {
+                updateAccountCredentials = new Intent(getContext(), AuthenticatorActivity.class);
+            } else {
+                updateAccountCredentials = new Intent(getContext(), ModifiedAuthenticatorActivity.class);
+            }
+
             updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, getAccount());
             updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
                     AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN);

+ 8 - 1
src/main/java/com/owncloud/android/ui/activity/FileActivity.java

@@ -40,6 +40,7 @@ import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.authentication.AuthenticatorActivity;
+import com.owncloud.android.authentication.ModifiedAuthenticatorActivity;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.ui.helpers.FileOperationsHelper;
 import com.owncloud.android.files.services.FileDownloader;
@@ -396,7 +397,13 @@ public abstract class FileActivity extends DrawerActivity
             }
 
             /// step 2 - request credentials to user
-            Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
+            Intent updateAccountCredentials;
+            if (!getResources().getBoolean(R.bool.push_enabled) &&
+                    !getResources().getBoolean(R.bool.analytics_enabled)) {
+                updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
+            } else {
+                updateAccountCredentials = new Intent(this, ModifiedAuthenticatorActivity.class);
+            }
             updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, account);
             updateAccountCredentials.putExtra(
                     AuthenticatorActivity.EXTRA_ACTION,

+ 10 - 1
src/main/java/com/owncloud/android/ui/activity/UserInfoActivity.java

@@ -48,6 +48,7 @@ import android.widget.TextView;
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.authentication.AuthenticatorActivity;
+import com.owncloud.android.authentication.ModifiedAuthenticatorActivity;
 import com.owncloud.android.lib.common.UserInfo;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
@@ -284,7 +285,15 @@ public class UserInfoActivity extends FileActivity {
     }
 
     private void changeAccountPassword(Account account) {
-        Intent updateAccountCredentials = new Intent(UserInfoActivity.this, AuthenticatorActivity.class);
+        // let the user update credentials with one click
+        Intent updateAccountCredentials;
+        if (!getResources().getBoolean(R.bool.push_enabled) &&
+                !getResources().getBoolean(R.bool.analytics_enabled)) {
+            updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
+        } else {
+            updateAccountCredentials = new Intent(this, ModifiedAuthenticatorActivity.class);
+        }
+
         updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, account);
         updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
                 AuthenticatorActivity.ACTION_UPDATE_TOKEN);

+ 1 - 1
src/main/res/values/setup.xml

@@ -118,7 +118,7 @@
 
     <!-- analytics enabled -->
     <bool name="analytics_enabled">false</bool>
-
+    <bool name="push_enabled">false</bool>
     <!-- Files becomes Home -->
     <bool name="use_home">false</bool>
 

+ 74 - 0
src/modified/AndroidManifest.xml

@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Nextcloud Android client application
+
+  Copyright (C) 2017 Mario Danic
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License version 2,
+  as published by the Free Software Foundation.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.owncloud.android"
+    android:versionCode="10040299"
+    android:versionName="1.4.2">
+
+    <application
+        android:name=".MainApp"
+        android:icon="@mipmap/ic_launcher"
+        android:label="@string/app_name"
+        android:fullBackupContent="@xml/backup_config"
+        android:theme="@style/Theme.ownCloud.Toolbar"
+        android:manageSpaceActivity="com.owncloud.android.ui.activity.ManageSpaceActivity">
+
+        <activity
+            android:name=".authentication.ModifiedAuthenticatorActivity"
+            android:exported="true"
+            android:launchMode="singleTask"
+            android:theme="@style/Theme.ownCloud.noActionBar.Login">
+            <intent-filter>
+                <action android:name="android.intent.action.VIEW" />
+
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.BROWSABLE" />
+
+                <data android:scheme="@string/oauth2_redirect_scheme" />
+            </intent-filter>
+            <intent-filter>
+                <action android:name="com.owncloud.android.workaround.accounts.CREATE" />
+
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+            <intent-filter>
+                <action android:name="android.intent.action.VIEW" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.BROWSABLE" />
+                <data android:scheme="@string/login_data_own_scheme" android:host="login"/>
+            </intent-filter>
+        </activity>
+
+        <service
+            android:name=".services.firebase.NCFirebaseMessagingService">
+            <intent-filter>
+                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
+            </intent-filter>
+        </service>
+
+        <service
+            android:name=".services.firebase.NCFirebaseInstanceIDService">
+            <intent-filter>
+                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
+            </intent-filter>
+        </service>
+
+    </application>
+
+</manifest>

+ 41 - 0
src/modified/java/com/owncloud/android/authentication/ModifiedAuthenticatorActivity.java

@@ -0,0 +1,41 @@
+package com.owncloud.android.authentication;
+
+import android.os.Bundle;
+
+import com.owncloud.android.utils.GooglePlayUtils;
+
+/**
+ * Nextcloud Android client application
+ *
+ * @author Mario Danic
+ * Copyright (C) 2017 Mario Danic
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+public class ModifiedAuthenticatorActivity extends AuthenticatorActivity {
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        GooglePlayUtils.checkPlayServices(this);
+    }
+
+    @Override
+    protected void onResume() {
+        super.onResume();
+        GooglePlayUtils.checkPlayServices(this);
+    }
+
+}

+ 47 - 0
src/modified/java/com/owncloud/android/services/firebase/NCFirebaseInstanceIDService.java

@@ -0,0 +1,47 @@
+/**
+ * Nextcloud Android client application
+ *
+ * @author Mario Danic
+ * Copyright (C) 2017 Mario Danic
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.owncloud.android.services.firebase;
+
+import android.util.Log;
+
+import com.google.firebase.iid.FirebaseInstanceId;
+import com.google.firebase.iid.FirebaseInstanceIdService;
+
+public class NCFirebaseInstanceIDService extends FirebaseInstanceIdService {
+    private static final String TAG = "NCFirebaseInstanceID";
+
+    @Override
+    public void onTokenRefresh() {
+
+        //Getting registration token
+        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
+
+        //Displaying token on logcat
+        Log.d(TAG, "Refreshed token: " + refreshedToken);
+
+    }
+
+    private void sendRegistrationToServer(String token) {
+        //You can implement this method to store the token on your server
+        //Not required for current project
+    }
+
+}
+

+ 71 - 0
src/modified/java/com/owncloud/android/services/firebase/NCFirebaseMessagingService.java

@@ -0,0 +1,71 @@
+/**
+ * Nextcloud Android client application
+ *
+ * @author Mario Danic
+ * Copyright (C) 2017 Mario Danic
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.owncloud.android.services.firebase;
+
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.Intent;
+import android.media.RingtoneManager;
+import android.net.Uri;
+import android.support.v4.app.NotificationCompat;
+import android.util.Log;
+
+import com.google.firebase.messaging.FirebaseMessagingService;
+import com.google.firebase.messaging.RemoteMessage;
+import com.owncloud.android.R;
+import com.owncloud.android.ui.activity.FileDisplayActivity;
+
+public class NCFirebaseMessagingService extends FirebaseMessagingService {
+    private static final String TAG = "NCFirebaseMessaging";
+
+    @Override
+    public void onMessageReceived(RemoteMessage remoteMessage) {
+        Log.d(TAG, "From: " + remoteMessage.getFrom());
+        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
+
+        //Calling method to generate notification
+        sendNotification(remoteMessage.getNotification().getBody());
+    }
+
+    //This method is only generating push notification
+    //It is same as we did in earlier posts
+    private void sendNotification(String messageBody) {
+        Intent intent = new Intent(this, FileDisplayActivity.class);
+        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
+                PendingIntent.FLAG_ONE_SHOT);
+
+        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
+        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
+                .setSmallIcon(R.mipmap.ic_launcher)
+                .setContentTitle("Firebase Push Notification")
+                .setContentText(messageBody)
+                .setAutoCancel(true)
+                .setSound(defaultSoundUri)
+                .setContentIntent(pendingIntent);
+
+        NotificationManager notificationManager =
+                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+
+        notificationManager.notify(0, notificationBuilder.build());
+    }
+
+}

+ 49 - 0
src/modified/java/com/owncloud/android/utils/GooglePlayUtils.java

@@ -0,0 +1,49 @@
+/**
+ * Nextcloud Android client application
+ *
+ * @author Mario Danic
+ * Copyright (C) 2017 Mario Danic
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.owncloud.android.utils;
+
+import android.app.Activity;
+import android.util.Log;
+
+import com.google.android.gms.common.ConnectionResult;
+import com.google.android.gms.common.GoogleApiAvailability;
+
+public class GooglePlayUtils {
+    private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
+    private static final String TAG = "GooglePlayUtils";
+
+    public static boolean checkPlayServices(Activity activity) {
+        GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
+        int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity);
+        if (resultCode != ConnectionResult.SUCCESS) {
+            if (apiAvailability.isUserResolvableError(resultCode)) {
+                apiAvailability.getErrorDialog(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
+                        .show();
+            } else {
+                Log.i(TAG, "This device is not supported.");
+                activity.finish();
+            }
+            return false;
+        }
+        return true;
+    }
+
+}

+ 1 - 0
src/modified/res/values/setup.xml

@@ -112,6 +112,7 @@
 
     <!-- analytics enabled -->
     <bool name="analytics_enabled">false</bool>
+    <bool name="push_enabled">true</bool>
 
     <!-- Files becomes Home -->
     <bool name="use_home">true</bool>