瀏覽代碼

Merge pull request #236 from nextcloud/davAppIntegration

DAVdroid login screen prefilled launch
Andy Scherzinger 8 年之前
父節點
當前提交
5a54b5967a

+ 2 - 1
.lgtm

@@ -1,2 +1,3 @@
 pattern = "(?i):shipit:|:\\+1:|LGTM"
-self_approval_off=true
+self_approval_off=true
+approvals = 1

+ 1 - 0
res/values/setup.xml

@@ -56,6 +56,7 @@
     <bool name="multiaccount_support">true</bool>
     
     <!-- Help, imprint and feedback -->
+    <bool name="calendar_contacts_enabled">true</bool>
     <bool name="help_enabled">true</bool>
     <bool name="imprint_enabled">false</bool> 
     <bool name="recommend_enabled">true</bool>

+ 8 - 3
res/values/strings.xml

@@ -1,5 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
 <resources>
-
     <string name="about_android">%1$s Android app</string>
     <string name="about_version">version %1$s</string>
     <string name="actionbar_sync">Refresh account</string>
@@ -37,6 +37,11 @@
     <string name="prefs_log_title_history">Logging history</string>
     <string name="prefs_log_summary_history">This shows the recorded logs</string>
     <string name="prefs_log_delete_history_button">Delete history</string>
+    <string name="prefs_calendar_contacts">Setup calendar &amp; contacts sync</string>
+    <string name="prefs_calendar_contacts_summary">Setup DAVdroid (v1.3.0+) for currently used account</string>
+    <string name="prefs_calendar_contacts_address_resolve_error">Server address for account could not be resolved for DAVdroid</string>
+    <string name="prefs_calendar_contacts_no_store_error">No Google Play store or F-Droid app installed</string>
+    <string name="prefs_calendar_contacts_sync_setup_successful">Calendar &amp; contacts sync set up successfully</string>
     <string name="prefs_help">Help</string>
     <string name="prefs_recommend">Recommend to a friend</string>
     <string name="prefs_feedback">Feedback</string>
@@ -406,8 +411,8 @@
     <string name="file_list__footer__files">%1$d files</string>
     <string name="file_list__footer__files_and_folder">%1$d files, 1 folder</string>
     <string name="file_list__footer__files_and_folders">%1$d files, %2$d folders</string>
-    <string name="prefs_instant_behaviour_dialogTitle">Original file will be...</string>
-    <string name="prefs_instant_behaviour_title">Original file will be...</string>
+    <string name="prefs_instant_behaviour_dialogTitle">Original file will be&#8230;</string>
+    <string name="prefs_instant_behaviour_title">Original file will be&#8230;</string>
     <string name="upload_copy_files">Copy file</string>
     <string name="upload_move_files">Move file</string>
     <string name="select_all">Select all</string>

+ 3 - 1
res/xml/preferences.xml

@@ -78,6 +78,9 @@
 	</PreferenceCategory>
 
 	<PreferenceCategory android:title="@string/prefs_category_more" android:key="more">
+		<Preference android:title="@string/prefs_calendar_contacts"
+					android:key="calendar_contacts"
+					android:summary="@string/prefs_calendar_contacts_summary" />
 		<Preference android:title="@string/prefs_help" android:key="help" />
 		<Preference android:title="@string/prefs_recommend" android:key="recommend" />
 		<Preference android:title="@string/prefs_feedback" android:key="feedback" />
@@ -86,5 +89,4 @@
 		<Preference android:title="@string/about_title" android:id="@+id/about_app" android:key="about_app" />
 	</PreferenceCategory>
 
-
 </PreferenceScreen>

+ 1 - 2
src/com/owncloud/android/ui/activity/DrawerActivity.java

@@ -268,8 +268,7 @@ public abstract class DrawerActivity extends ToolbarActivity implements DisplayU
                                 startActivity(uploadListIntent);
                                 break;
                             case R.id.nav_settings:
-                                Intent settingsIntent = new Intent(getApplicationContext(),
-                                        Preferences.class);
+                                Intent settingsIntent = new Intent(getApplicationContext(), Preferences.class);
                                 startActivity(settingsIntent);
                                 break;
                             case R.id.nav_participate:

+ 102 - 2
src/com/owncloud/android/ui/activity/Preferences.java

@@ -22,6 +22,8 @@
 package com.owncloud.android.ui.activity;
 
 import android.accounts.Account;
+import android.accounts.AuthenticatorException;
+import android.accounts.OperationCanceledException;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.pm.PackageInfo;
@@ -49,12 +51,17 @@ import android.view.ViewGroup;
 import android.widget.Toast;
 
 import com.owncloud.android.BuildConfig;
+import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.lib.common.OwnCloudAccount;
+import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.utils.DisplayUtils;
 
+import java.io.IOException;
+
 
 /**
  * An Activity that allows the user to change the application's settings.
@@ -71,6 +78,13 @@ public class Preferences extends PreferenceActivity {
     private static final int ACTION_REQUEST_PASSCODE = 5;
     private static final int ACTION_CONFIRM_PASSCODE = 6;
 
+    private static final int ACTION_REQUEST_CODE_DAVDROID_SETUP = 10;
+
+    /**
+     * The user's server base uri.
+     */
+    private Uri mUri;
+
     private CheckBoxPreference pCode;
     private Preference pAboutApp;
     private AppCompatDelegate mDelegate;
@@ -103,6 +117,9 @@ public class Preferences extends PreferenceActivity {
         actionBar.setDisplayHomeAsUpEnabled(true);
         actionBar.setTitle(R.string.actionbar_settings);
 
+        // retrieve user's base uri
+        setupBaseUri();
+
         // For adding content description tag to a title field in the action bar
         int actionBarTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
         View actionBarTitleView = getWindow().getDecorView().findViewById(actionBarTitleId);
@@ -148,6 +165,31 @@ public class Preferences extends PreferenceActivity {
         }
 
         PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("more");
+
+        boolean calendarContactsEnabled = getResources().getBoolean(R.bool.calendar_contacts_enabled);
+        Preference pCalendarContacts = findPreference("calendar_contacts");
+        if (pCalendarContacts != null) {
+            if (calendarContactsEnabled) {
+                pCalendarContacts.setOnPreferenceClickListener(new OnPreferenceClickListener() {
+                    @Override
+                    public boolean onPreferenceClick(Preference preference) {
+                        try {
+                            launchDavDroidLogin();
+                        } catch (Throwable t) {
+                            Log_OC.e(TAG, "Base Uri for account could not be resolved to call DAVdroid!", t);
+                            Toast.makeText(
+                                    MainApp.getAppContext(),
+                                    R.string.prefs_calendar_contacts_address_resolve_error,
+                                    Toast.LENGTH_SHORT)
+                                    .show();
+                        }
+                        return true;
+                    }
+                });
+            } else {
+                preferenceCategory.removePreference(pCalendarContacts);
+            }
+        }
         
         boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
         Preference pHelp =  findPreference("help");
@@ -362,6 +404,62 @@ public class Preferences extends PreferenceActivity {
        loadInstantUploadPath();
        loadInstantUploadVideoPath();
     }
+
+    private void launchDavDroidLogin()
+            throws com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException,
+            OperationCanceledException,
+            AuthenticatorException,
+            IOException {
+        Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
+
+        Intent davDroidLoginIntent = new Intent();
+        davDroidLoginIntent.setClassName("at.bitfire.davdroid", "at.bitfire.davdroid.ui.setup.LoginActivity");
+        if (getPackageManager().resolveActivity(davDroidLoginIntent, 0) != null) {
+            // arguments
+            if (mUri != null) {
+                davDroidLoginIntent.putExtra("url", mUri.toString());
+            }
+            davDroidLoginIntent.putExtra("username", AccountUtils.getAccountUsername(account.name));
+            //loginIntent.putExtra("password", "...");
+            startActivityForResult(davDroidLoginIntent, ACTION_REQUEST_CODE_DAVDROID_SETUP);
+        } else {
+            // DAVdroid not installed
+            Intent installIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=at.bitfire.davdroid"));
+
+            // launch market(s)
+            if (installIntent.resolveActivity(getPackageManager()) != null) {
+                startActivity(installIntent);
+            } else {
+                // no f-droid market app or Play store installed --> launch browser for f-droid url
+                Intent downloadIntent = new Intent(Intent.ACTION_VIEW,
+                        Uri.parse("https://f-droid.org/repository/browse/?fdid=at.bitfire.davdroid"));
+                startActivity(downloadIntent);
+
+                Toast.makeText(
+                        MainApp.getAppContext(),
+                        R.string.prefs_calendar_contacts_no_store_error,
+                        Toast.LENGTH_SHORT)
+                        .show();
+            }
+        }
+    }
+
+    private void setupBaseUri() {
+        // retrieve and set user's base URI
+        Thread t = new Thread(new Runnable() {
+            public void run() {
+                try {
+                    Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
+                    OwnCloudAccount ocAccount = new OwnCloudAccount(account, MainApp.getAppContext());
+                    mUri = OwnCloudClientManagerFactory.getDefaultSingleton().
+                            getClientFor(ocAccount, getApplicationContext()).getBaseUri();
+                } catch (Throwable t) {
+                    Log_OC.e(TAG,"Error retrieving user's base URI", t);
+                }
+            }
+        });
+        t.start();
+    }
     
     private void toggleInstantPictureOptions(Boolean value){
         if (value){
@@ -473,7 +571,7 @@ public class Preferences extends PreferenceActivity {
                     appPrefs.putString(PassCodeActivity.PREFERENCE_PASSCODE_D + i, passcode.substring(i-1, i));
                 }
                 appPrefs.putBoolean(PassCodeActivity.PREFERENCE_SET_PASSCODE, true);
-                appPrefs.commit();
+                appPrefs.apply();
                 Toast.makeText(this, R.string.pass_code_stored, Toast.LENGTH_LONG).show();
             }
         } else if (requestCode == ACTION_CONFIRM_PASSCODE && resultCode == RESULT_OK) {
@@ -482,10 +580,12 @@ public class Preferences extends PreferenceActivity {
                 SharedPreferences.Editor appPrefs = PreferenceManager
                         .getDefaultSharedPreferences(getApplicationContext()).edit();
                 appPrefs.putBoolean(PassCodeActivity.PREFERENCE_SET_PASSCODE, false);
-                appPrefs.commit();
+                appPrefs.apply();
 
                 Toast.makeText(this, R.string.pass_code_removed, Toast.LENGTH_LONG).show();
             }
+        } else if (requestCode == ACTION_REQUEST_CODE_DAVDROID_SETUP && resultCode == RESULT_OK) {
+            Toast.makeText(this, R.string.prefs_calendar_contacts_sync_setup_successful, Toast.LENGTH_LONG).show();
         }
     }