浏览代码

external links are loaded as soon as possible, important for very first start

tobiasKaminsky 8 年之前
父节点
当前提交
f59e80eda4

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

@@ -26,12 +26,14 @@ import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.accounts.AccountManagerFuture;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.content.res.Configuration;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.PictureDrawable;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
+import android.preference.PreferenceManager;
 import android.support.design.widget.NavigationView;
 import android.support.v4.view.GravityCompat;
 import android.support.v4.widget.DrawerLayout;
@@ -56,11 +58,13 @@ import com.owncloud.android.lib.common.ExternalLinkType;
 import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.Quota;
 import com.owncloud.android.lib.common.UserInfo;
+import com.owncloud.android.lib.common.accounts.ExternalLinksOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.resources.files.SearchOperation;
 import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;
+import com.owncloud.android.operations.GetCapabilitiesOperarion;
 import com.owncloud.android.ui.TextDrawable;
 import com.owncloud.android.ui.events.ChangeMenuEvent;
 import com.owncloud.android.ui.events.DummyDrawerEvent;
@@ -82,6 +86,7 @@ public abstract class DrawerActivity extends ToolbarActivity implements DisplayU
     private static final String TAG = DrawerActivity.class.getSimpleName();
     private static final String KEY_IS_ACCOUNT_CHOOSER_ACTIVE = "IS_ACCOUNT_CHOOSER_ACTIVE";
     private static final String KEY_CHECKED_MENU_ITEM = "CHECKED_MENU_ITEM";
+    private static final String EXTERNAL_LINKS_COUNT = "EXTERNAL_LINKS_COUNT";
     private static final int ACTION_MANAGE_ACCOUNTS = 101;
     private static final int MENU_ORDER_ACCOUNT = 1;
     private static final int MENU_ORDER_ACCOUNT_FUNCTION = 2;
@@ -168,6 +173,7 @@ public abstract class DrawerActivity extends ToolbarActivity implements DisplayU
     private Runnable pendingRunnable;
 
     private ExternalLinksProvider externalLinksProvider;
+    private SharedPreferences sharedPreferences;
 
     /**
      * Initializes the drawer, its content and highlights the menu item with the given id.
@@ -490,6 +496,9 @@ public abstract class DrawerActivity extends ToolbarActivity implements DisplayU
     private void accountClicked(String accountName) {
         if (!AccountUtils.getCurrentOwnCloudAccount(getApplicationContext()).name.equals(accountName)) {
             AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), accountName);
+
+            fetchExternalLinks(true);
+
             restart();
         }
     }
@@ -932,8 +941,9 @@ public abstract class DrawerActivity extends ToolbarActivity implements DisplayU
         mMenuAccountAvatarRadiusDimension = getResources()
                 .getDimension(R.dimen.nav_drawer_menu_avatar_radius);
 
-        externalLinksProvider =
-                new ExternalLinksProvider(MainApp.getAppContext().getContentResolver());
+        externalLinksProvider = new ExternalLinksProvider(MainApp.getAppContext().getContentResolver());
+
+        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
     }
 
     @Override
@@ -1111,4 +1121,59 @@ public abstract class DrawerActivity extends ToolbarActivity implements DisplayU
         EventBus.getDefault().unregister(this);
         super.onStop();
     }
+
+    /**
+     * Retrieves external links via api from 'external' app
+     */
+    public void fetchExternalLinks(final boolean force) {
+        if (getBaseContext().getResources().getBoolean(R.bool.show_external_links)) {
+            Thread t = new Thread(new Runnable() {
+                public void run() {
+                    // fetch capabilities as early as possible
+                    if ((getCapabilities() == null || getCapabilities().getAccountName().isEmpty())
+                            && getStorageManager() != null) {
+                        GetCapabilitiesOperarion getCapabilities = new GetCapabilitiesOperarion();
+                        getCapabilities.execute(getStorageManager(), getBaseContext());
+                    }
+
+                    Account account = AccountUtils.getCurrentOwnCloudAccount(DrawerActivity.this);
+
+                    if (account != null && getStorageManager().getCapability(account.name) != null &&
+                            getStorageManager().getCapability(account.name).getExternalLinks().isTrue()) {
+
+                        int count = sharedPreferences.getInt(EXTERNAL_LINKS_COUNT, -1);
+                        if (count > 10 || count == -1 || force) {
+                            if (force) {
+                                Log_OC.d("ExternalLinks", "force update");
+                            }
+
+                            sharedPreferences.edit().putInt(EXTERNAL_LINKS_COUNT, 0).apply();
+
+                            Log_OC.d("ExternalLinks", "update via api");
+                            ExternalLinksProvider externalLinksProvider = new ExternalLinksProvider(getContentResolver());
+
+                            RemoteOperation getExternalLinksOperation = new ExternalLinksOperation();
+                            RemoteOperationResult result = getExternalLinksOperation.execute(account, DrawerActivity.this);
+
+                            if (result.isSuccess() && result.getData() != null) {
+                                externalLinksProvider.deleteAllExternalLinks();
+
+                                ArrayList<ExternalLink> externalLinks = (ArrayList<ExternalLink>) (Object) result.getData();
+
+                                for (ExternalLink link : externalLinks) {
+                                    externalLinksProvider.storeExternalLink(link);
+                                }
+                            }
+                        } else {
+                            sharedPreferences.edit().putInt(EXTERNAL_LINKS_COUNT, count + 1).apply();
+                        }
+                    } else {
+                        Log_OC.d("ExternalLinks", "links disabled");
+                    }
+                }
+            });
+
+            t.start();
+        }
+    }
 }

+ 3 - 52
src/main/java/com/owncloud/android/ui/activity/FileActivity.java

@@ -28,11 +28,9 @@ import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.ServiceConnection;
-import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
-import android.preference.PreferenceManager;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentManager;
 import android.support.v4.app.FragmentTransaction;
@@ -42,18 +40,15 @@ 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.datamodel.ExternalLinksProvider;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
-import com.owncloud.android.lib.common.ExternalLink;
 import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
 import com.owncloud.android.lib.common.OwnCloudCredentials;
-import com.owncloud.android.lib.common.accounts.ExternalLinksOperation;
 import com.owncloud.android.lib.common.network.CertificateCombinedException;
 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
@@ -75,8 +70,6 @@ import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;
 import com.owncloud.android.ui.helpers.FileOperationsHelper;
 import com.owncloud.android.utils.ErrorMessageAdapter;
 
-import java.util.ArrayList;
-
 
 /**
  * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
@@ -105,8 +98,6 @@ public abstract class FileActivity extends DrawerActivity
     private static final String DIALOG_UNTRUSTED_CERT = "DIALOG_UNTRUSTED_CERT";
     private static final String DIALOG_CERT_NOT_SAVED = "DIALOG_CERT_NOT_SAVED";
 
-    private static final String EXTERNAL_LINKS_COUNT = "EXTERNAL_LINKS_COUNT";
-
      /** Main {@link OCFile} handled by the activity.*/
     private OCFile mFile;
 
@@ -128,7 +119,7 @@ public abstract class FileActivity extends DrawerActivity
     protected FileUploaderBinder mUploaderBinder = null;
     private ServiceConnection mDownloadServiceConnection, mUploadServiceConnection = null;
 
-    private SharedPreferences sharedPreferences;
+
 
     @Override
     public void showFiles(boolean onDeviceOnly) {
@@ -184,57 +175,17 @@ public abstract class FileActivity extends DrawerActivity
             bindService(new Intent(this, FileUploader.class), mUploadServiceConnection,
                     Context.BIND_AUTO_CREATE);
         }
-
-        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
     }
 
     @Override
     protected void onStart() {
         super.onStart();
 
-        getAndDisplayExternalLinks();
+        fetchExternalLinks(false);
     }
 
 
-    /**
-     * Retrieves external links via api from 'external' app
-     */
-    public void getAndDisplayExternalLinks() {
-        Thread t = new Thread(new Runnable() {
-            public void run() {
-                if (getCapabilities() != null && getCapabilities().getExternalLinks().isTrue()) {
-
-                    int count = sharedPreferences.getInt(EXTERNAL_LINKS_COUNT, 0);
-                    if (count > 10) {
-                        sharedPreferences.edit().putInt(EXTERNAL_LINKS_COUNT, 0).apply();
-
-                        Log_OC.d("ExternalLinks", "update via api");
-                        ExternalLinksProvider externalLinksProvider = new ExternalLinksProvider(getContentResolver());
-
-                        RemoteOperation getExternalLinksOperation = new ExternalLinksOperation();
-                        RemoteOperationResult result = getExternalLinksOperation.execute(
-                                AccountUtils.getCurrentOwnCloudAccount(FileActivity.this), FileActivity.this);
 
-                        if (result.isSuccess() && result.getData() != null) {
-                            externalLinksProvider.deleteAllExternalLinks();
-
-                            ArrayList<ExternalLink> externalLinks = (ArrayList<ExternalLink>) (Object) result.getData();
-
-                            for (ExternalLink link : externalLinks) {
-                                externalLinksProvider.storeExternalLink(link);
-                            }
-                        }
-                    } else {
-                        sharedPreferences.edit().putInt(EXTERNAL_LINKS_COUNT, count + 1).apply();
-                    }
-                } else {
-                    Log_OC.d("ExternalLinks", "links disabled");
-                }
-            }
-        });
-
-        t.start();
-    }
 
     @Override
     protected void onResume() {
@@ -603,7 +554,7 @@ public abstract class FileActivity extends DrawerActivity
         i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         startActivity(i);
 
-        getAndDisplayExternalLinks();
+        fetchExternalLinks(false);
     }
 
     protected OCFile getCurrentDir() {