Przeglądaj źródła

Init auto upload on boot

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 7 lat temu
rodzic
commit
d69f0f0a55

+ 3 - 0
src/main/AndroidManifest.xml

@@ -235,6 +235,9 @@
         <receiver android:name=".files.BootupBroadcastReceiver" >
             <intent-filter>
                 <action android:name="android.intent.action.BOOT_COMPLETED" />
+                <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
+                <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
+                <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
             </intent-filter>
         </receiver>
 

+ 49 - 38
src/main/java/com/owncloud/android/MainApp.java

@@ -125,26 +125,7 @@ public class MainApp extends MultiDexApplication {
             Log_OC.d("Debug", "start logging");
         }
 
-        updateToAutoUpload();
-        cleanOldEntries();
-        updateAutoUploadEntries();
-
-        if (PermissionUtil.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
-            splitOutAutoUploadEntries();
-        } else {
-            PreferenceManager.setAutoUploadSplitEntries(this, true);
-        }
-
-        initiateExistingAutoUploadEntries();
-
-        FilesSyncHelper.scheduleFilesSyncIfNeeded();
-        FilesSyncHelper.restartJobsIfNeeded();
-
-        ReceiversHelper.registerNetworkChangeReceiver();
-
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
-            ReceiversHelper.registerPowerChangeReceiver();
-        }
+        initAutoUpload();
 
         // register global protection with pass code
         registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@@ -190,6 +171,31 @@ public class MainApp extends MultiDexApplication {
         });
     }
 
+    public static void initAutoUpload() {
+        updateToAutoUpload();
+        cleanOldEntries();
+        updateAutoUploadEntries();
+
+        if (getAppContext() != null) {
+            if (PermissionUtil.checkSelfPermission(getAppContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
+                splitOutAutoUploadEntries();
+            } else {
+                PreferenceManager.setAutoUploadSplitEntries(getAppContext(), true);
+            }
+        }
+
+        initiateExistingAutoUploadEntries();
+
+        FilesSyncHelper.scheduleFilesSyncIfNeeded();
+        FilesSyncHelper.restartJobsIfNeeded();
+
+        ReceiversHelper.registerNetworkChangeReceiver();
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
+            ReceiversHelper.registerPowerChangeReceiver();
+        }
+    }
+
     public static Context getAppContext() {
         return MainApp.mContext;
     }
@@ -301,12 +307,13 @@ public class MainApp extends MultiDexApplication {
         return userAgent;
     }
 
-    private void updateToAutoUpload() {
-            if (PreferenceManager.instantPictureUploadEnabled(this) ||
-                            PreferenceManager.instantPictureUploadEnabled(this)) {
+    private static void updateToAutoUpload() {
+            Context context = getAppContext();
+            if (PreferenceManager.instantPictureUploadEnabled(context) ||
+                            PreferenceManager.instantPictureUploadEnabled(context)) {
 
                 // remove legacy shared preferences
-                SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
+                SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
                 editor.remove("instant_uploading")
                         .remove("instant_video_uploading")
                         .remove("instant_upload_path")
@@ -322,16 +329,16 @@ public class MainApp extends MultiDexApplication {
 
                 // show info pop-up
                 try {
-                    new AlertDialog.Builder(this, R.style.Theme_ownCloud_Dialog)
+                    new AlertDialog.Builder(context, R.style.Theme_ownCloud_Dialog)
                             .setTitle(R.string.drawer_synced_folders)
                             .setMessage(R.string.synced_folders_new_info)
                             .setPositiveButton(R.string.drawer_open, new DialogInterface.OnClickListener() {
                                 public void onClick(DialogInterface dialog, int which) {
                                     // show Auto Upload
-                                    Intent folderSyncIntent = new Intent(getApplicationContext(),
+                                    Intent folderSyncIntent = new Intent(context,
                                             SyncedFoldersActivity.class);
                                     dialog.dismiss();
-                                    startActivity(folderSyncIntent);
+                                    context.startActivity(folderSyncIntent);
                                 }
                             })
                             .setNegativeButton(R.string.drawer_close, new DialogInterface.OnClickListener() {
@@ -347,21 +354,23 @@ public class MainApp extends MultiDexApplication {
             }
     }
 
-    private void updateAutoUploadEntries() {
+    private static void updateAutoUploadEntries() {
         // updates entries to reflect their true paths
-        if (!PreferenceManager.getAutoUploadPathsUpdate(this)) {
+        Context context = getAppContext();
+        if (!PreferenceManager.getAutoUploadPathsUpdate(context)) {
             SyncedFolderProvider syncedFolderProvider =
                     new SyncedFolderProvider(MainApp.getAppContext().getContentResolver());
             syncedFolderProvider.updateAutoUploadPaths(mContext);
         }
     }
 
-    private void splitOutAutoUploadEntries() {
-        if (!PreferenceManager.getAutoUploadSplitEntries(this)) {
+    private static void splitOutAutoUploadEntries() {
+        Context context = getAppContext();
+        if (!PreferenceManager.getAutoUploadSplitEntries(context)) {
             // magic to split out existing synced folders in two when needed
             // otherwise, we migrate them to their proper type (image or video)
             Log_OC.i(TAG, "Migrate synced_folders records for image/video split");
-            ContentResolver contentResolver = this.getContentResolver();
+            ContentResolver contentResolver = context.getContentResolver();
 
             SyncedFolderProvider syncedFolderProvider = new SyncedFolderProvider(contentResolver);
 
@@ -405,11 +414,11 @@ public class MainApp extends MultiDexApplication {
                 syncedFolderProvider.deleteSyncedFolder(id);
             }
 
-            PreferenceManager.setAutoUploadSplitEntries(this, true);
+            PreferenceManager.setAutoUploadSplitEntries(context, true);
         }
     }
 
-    private void initiateExistingAutoUploadEntries() {
+    private static void initiateExistingAutoUploadEntries() {
         new Thread(() -> {
             if (!PreferenceManager.getAutoUploadInit(getAppContext())) {
                 SyncedFolderProvider syncedFolderProvider =
@@ -427,13 +436,15 @@ public class MainApp extends MultiDexApplication {
         }).start();
     }
 
-    private void cleanOldEntries() {
+    private static void cleanOldEntries() {
         // previous versions of application created broken entries in the SyncedFolderProvider
         // database, and this cleans all that and leaves 1 (newest) entry per synced folder
 
-        if (!PreferenceManager.getLegacyClean(this)) {
+        Context context = getAppContext();
+
+        if (!PreferenceManager.getLegacyClean(context)) {
             SyncedFolderProvider syncedFolderProvider =
-                    new SyncedFolderProvider(MainApp.getAppContext().getContentResolver());
+                    new SyncedFolderProvider(context.getContentResolver());
 
             List<SyncedFolder> syncedFolderList = syncedFolderProvider.getSyncedFolders();
             Map<Pair<String, String>, Long> syncedFolders = new HashMap<>();
@@ -454,7 +465,7 @@ public class MainApp extends MultiDexApplication {
             if (ids.size() > 0) {
                 syncedFolderProvider.deleteSyncedFoldersNotInList(mContext, ids);
             } else {
-                PreferenceManager.setLegacyClean(this, true);
+                PreferenceManager.setLegacyClean(context, true);
             }
         }
     }

+ 4 - 4
src/main/java/com/owncloud/android/files/BootupBroadcastReceiver.java

@@ -4,6 +4,7 @@
  * @author David A. Velasco
  * Copyright (C) 2012 Bartek Przybylski
  * Copyright (C) 2015 ownCloud Inc.
+ * 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,
@@ -24,6 +25,7 @@ import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 
+import com.owncloud.android.MainApp;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.services.observer.FileObserverService;
 
@@ -46,13 +48,11 @@ public class BootupBroadcastReceiver extends BroadcastReceiver {
      */
     @Override
     public void onReceive(Context context, Intent intent) {
-        if (!intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
-            Log_OC.e(TAG, "Incorrect action sent " + intent.getAction());
-            return;
-        }
         Log_OC.d(TAG, "Starting file observer service...");
         Intent initObservers = FileObserverService.makeInitIntent(context);
         context.startService(initObservers);
+
+        MainApp.initAutoUpload();
     }
 
 }