瀏覽代碼

"backup now" asks for permission

tobiasKaminsky 8 年之前
父節點
當前提交
fa414c2a1b

+ 24 - 5
src/main/java/com/owncloud/android/ui/activity/ContactsPreferenceActivity.java

@@ -93,7 +93,8 @@ public class ContactsPreferenceActivity extends FileActivity implements FileFrag
         backupSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
             @Override
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
-                if (isChecked && checkAndAskForContactsReadPermission()) {
+                if (isChecked &&
+                        checkAndAskForContactsReadPermission(PermissionUtil.PERMISSIONS_READ_CONTACTS_AUTOMATIC)) {
                     // store value
                     setAutomaticBackup(backupSwitch, true);
 
@@ -123,7 +124,7 @@ public class ContactsPreferenceActivity extends FileActivity implements FileFrag
     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
 
-        if (requestCode == PermissionUtil.PERMISSIONS_READ_CONTACTS) {
+        if (requestCode == PermissionUtil.PERMISSIONS_READ_CONTACTS_AUTOMATIC) {
             for (int index = 0; index < permissions.length; index++) {
                 if (Manifest.permission.READ_CONTACTS.equalsIgnoreCase(permissions[index])) {
                     if (grantResults[index] >= 0) {
@@ -136,9 +137,27 @@ public class ContactsPreferenceActivity extends FileActivity implements FileFrag
                 }
             }
         }
+
+        if (requestCode == PermissionUtil.PERMISSIONS_READ_CONTACTS_MANUALLY) {
+            for (int index = 0; index < permissions.length; index++) {
+                if (Manifest.permission.READ_CONTACTS.equalsIgnoreCase(permissions[index])) {
+                    if (grantResults[index] >= 0) {
+                        startContactsBackupJob();
+                    }
+
+                    break;
+                }
+            }
+        }
     }
 
     public void backupContacts(View v) {
+        if (checkAndAskForContactsReadPermission(PermissionUtil.PERMISSIONS_READ_CONTACTS_MANUALLY)) {
+            startContactsBackupJob();
+        }
+    }
+
+    private void startContactsBackupJob() {
         PersistableBundleCompat bundle = new PersistableBundleCompat();
         bundle.putString(ContactsBackupJob.ACCOUNT, getAccount().name);
         bundle.putBoolean(ContactsBackupJob.FORCE, true);
@@ -163,7 +182,7 @@ public class ContactsPreferenceActivity extends FileActivity implements FileFrag
         editor.apply();
     }
 
-    private boolean checkAndAskForContactsReadPermission() {
+    private boolean checkAndAskForContactsReadPermission(final int permission) {
         // check permissions
         if ((PermissionUtil.checkSelfPermission(this, Manifest.permission.READ_CONTACTS))) {
             return true;
@@ -177,7 +196,7 @@ public class ContactsPreferenceActivity extends FileActivity implements FileFrag
                         .setAction(R.string.common_ok, new View.OnClickListener() {
                             @Override
                             public void onClick(View v) {
-                                PermissionUtil.requestReadContactPermission(ContactsPreferenceActivity.this);
+                                PermissionUtil.requestReadContactPermission(ContactsPreferenceActivity.this, permission);
                             }
                         });
 
@@ -188,7 +207,7 @@ public class ContactsPreferenceActivity extends FileActivity implements FileFrag
                 return false;
             } else {
                 // No explanation needed, request the permission.
-                PermissionUtil.requestReadContactPermission(ContactsPreferenceActivity.this);
+                PermissionUtil.requestReadContactPermission(ContactsPreferenceActivity.this, permission);
 
                 return false;
             }

+ 5 - 4
src/main/java/com/owncloud/android/utils/PermissionUtil.java

@@ -11,8 +11,9 @@ import android.support.v4.content.ContextCompat;
  */
 public class PermissionUtil {
     public static final int PERMISSIONS_WRITE_EXTERNAL_STORAGE = 1;
-    public static final int PERMISSIONS_READ_CONTACTS = 2;
-    public static final int PERMISSIONS_WRITE_CONTACTS = 3;
+    public static final int PERMISSIONS_READ_CONTACTS_AUTOMATIC = 2;
+    public static final int PERMISSIONS_READ_CONTACTS_MANUALLY = 3;
+    public static final int PERMISSIONS_WRITE_CONTACTS = 4;
 
     /**
      * Wrapper method for ContextCompat.checkSelfPermission().
@@ -58,10 +59,10 @@ public class PermissionUtil {
      *
      * @param activity The target activity.
      */
-    public static void requestReadContactPermission(Activity activity) {
+    public static void requestReadContactPermission(Activity activity, int permission) {
         ActivityCompat.requestPermissions(activity,
                 new String[]{Manifest.permission.READ_CONTACTS},
-                PERMISSIONS_READ_CONTACTS);
+                permission);
     }
 
     /**