OSDN Git Service

Begin refactor of backup / restore logic
authorBraden Farmer <farmerbb@gmail.com>
Sun, 5 Jan 2020 06:12:06 +0000 (23:12 -0700)
committerBraden Farmer <farmerbb@gmail.com>
Sun, 5 Jan 2020 06:12:06 +0000 (23:12 -0700)
app/src/main/java/com/farmerbb/taskbar/backup/BackupAgent.java [new file with mode: 0644]
app/src/main/java/com/farmerbb/taskbar/backup/BackupUtils.java [new file with mode: 0644]
app/src/main/java/com/farmerbb/taskbar/backup/IntentBackupAgent.java [new file with mode: 0644]
app/src/main/java/com/farmerbb/taskbar/backup/JSONBackupAgent.java [new file with mode: 0644]
app/src/playstore/java/com/farmerbb/taskbar/receiver/ReceiveSettingsReceiver.java
app/src/playstore/java/com/farmerbb/taskbar/receiver/SendSettingsReceiver.java

diff --git a/app/src/main/java/com/farmerbb/taskbar/backup/BackupAgent.java b/app/src/main/java/com/farmerbb/taskbar/backup/BackupAgent.java
new file mode 100644 (file)
index 0000000..507c6a3
--- /dev/null
@@ -0,0 +1,27 @@
+/* Copyright 2020 Braden Farmer
+ *
+ * Licensed under the Apache License, Version 2.0(the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.farmerbb.taskbar.backup;
+
+public interface BackupAgent {
+
+    void putString(String key, String value);
+    void putStringArray(String key, String[] value);
+    void putLongArray(String key, long[] value);
+
+    String getString(String key);
+    String[] getStringArray(String key);
+    long[] getLongArray(String key);
+}
\ No newline at end of file
diff --git a/app/src/main/java/com/farmerbb/taskbar/backup/BackupUtils.java b/app/src/main/java/com/farmerbb/taskbar/backup/BackupUtils.java
new file mode 100644 (file)
index 0000000..32aebe4
--- /dev/null
@@ -0,0 +1,270 @@
+/* Copyright 2020 Braden Farmer
+ *
+ * Licensed under the Apache License, Version 2.0(the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.farmerbb.taskbar.backup;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.LauncherApps;
+import android.os.Process;
+import android.os.UserManager;
+
+import com.farmerbb.taskbar.util.AppEntry;
+import com.farmerbb.taskbar.util.Blacklist;
+import com.farmerbb.taskbar.util.BlacklistEntry;
+import com.farmerbb.taskbar.util.IconCache;
+import com.farmerbb.taskbar.util.PinnedBlockedApps;
+import com.farmerbb.taskbar.util.SavedWindowSizes;
+import com.farmerbb.taskbar.util.SavedWindowSizesEntry;
+import com.farmerbb.taskbar.util.TopApps;
+import com.farmerbb.taskbar.util.U;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.List;
+
+public class BackupUtils {
+
+    private BackupUtils() {}
+
+    public static void backup(Context context, BackupAgent agent) {
+        // Get pinned and blocked apps
+        PinnedBlockedApps pba = PinnedBlockedApps.getInstance(context);
+        List<AppEntry> pinnedAppsList = pba.getPinnedApps();
+
+        String[] pinnedAppsPackageNames = new String[pinnedAppsList.size()];
+        String[] pinnedAppsComponentNames = new String[pinnedAppsList.size()];
+        String[] pinnedAppsLabels = new String[pinnedAppsList.size()];
+        long[] pinnedAppsUserIds = new long[pinnedAppsList.size()];
+
+        for(int i = 0; i < pinnedAppsList.size(); i++) {
+            AppEntry entry = pinnedAppsList.get(i);
+            pinnedAppsPackageNames[i] = entry.getPackageName();
+            pinnedAppsComponentNames[i] = entry.getComponentName();
+            pinnedAppsLabels[i] = entry.getLabel();
+            pinnedAppsUserIds[i] = entry.getUserId(context);
+        }
+
+        agent.putStringArray("pinned_apps_package_names", pinnedAppsPackageNames);
+        agent.putStringArray("pinned_apps_component_names", pinnedAppsComponentNames);
+        agent.putStringArray("pinned_apps_labels", pinnedAppsLabels);
+        agent.putLongArray("pinned_apps_user_ids", pinnedAppsUserIds);
+
+        List<AppEntry> blockedAppsList = pba.getBlockedApps();
+
+        String[] blockedAppsPackageNames = new String[blockedAppsList.size()];
+        String[] blockedAppsComponentNames = new String[blockedAppsList.size()];
+        String[] blockedAppsLabels = new String[blockedAppsList.size()];
+
+        for(int i = 0; i < blockedAppsList.size(); i++) {
+            AppEntry entry = blockedAppsList.get(i);
+            blockedAppsPackageNames[i] = entry.getPackageName();
+            blockedAppsComponentNames[i] = entry.getComponentName();
+            blockedAppsLabels[i] = entry.getLabel();
+        }
+
+        agent.putStringArray("blocked_apps_package_names", blockedAppsPackageNames);
+        agent.putStringArray("blocked_apps_component_names", blockedAppsComponentNames);
+        agent.putStringArray("blocked_apps_labels", blockedAppsLabels);
+
+        // Get blacklist
+        Blacklist blacklist = Blacklist.getInstance(context);
+        List<BlacklistEntry> blacklistList = blacklist.getBlockedApps();
+
+        String[] blacklistPackageNames = new String[blacklistList.size()];
+        String[] blacklistLabels = new String[blacklistList.size()];
+
+        for(int i = 0; i < blacklistList.size(); i++) {
+            BlacklistEntry entry = blacklistList.get(i);
+            blacklistPackageNames[i] = entry.getPackageName();
+            blacklistLabels[i] = entry.getLabel();
+        }
+
+        agent.putStringArray("blacklist_package_names", blacklistPackageNames);
+        agent.putStringArray("blacklist_labels", blacklistLabels);
+
+        // Get top apps
+        TopApps topApps = TopApps.getInstance(context);
+        List<BlacklistEntry> topAppsList = topApps.getTopApps();
+
+        String[] topAppsPackageNames = new String[topAppsList.size()];
+        String[] topAppsLabels = new String[topAppsList.size()];
+
+        for(int i = 0; i < topAppsList.size(); i++) {
+            BlacklistEntry entry = topAppsList.get(i);
+            topAppsPackageNames[i] = entry.getPackageName();
+            topAppsLabels[i] = entry.getLabel();
+        }
+
+        agent.putStringArray("top_apps_package_names", topAppsPackageNames);
+        agent.putStringArray("top_apps_labels", topAppsLabels);
+
+        // Get saved window sizes
+        if(U.canEnableFreeform()) {
+            SavedWindowSizes savedWindowSizes = SavedWindowSizes.getInstance(context);
+            List<SavedWindowSizesEntry> savedWindowSizesList = savedWindowSizes.getSavedWindowSizes();
+
+            String[] savedWindowSizesComponentNames = new String[savedWindowSizesList.size()];
+            String[] savedWindowSizesWindowSizes = new String[savedWindowSizesList.size()];
+
+            for(int i = 0; i < savedWindowSizesList.size(); i++) {
+                SavedWindowSizesEntry entry = savedWindowSizesList.get(i);
+                savedWindowSizesComponentNames[i] = entry.getComponentName();
+                savedWindowSizesWindowSizes[i] = entry.getWindowSize();
+            }
+
+            agent.putStringArray("saved_window_sizes_component_names", savedWindowSizesComponentNames);
+            agent.putStringArray("saved_window_sizes_window_sizes", savedWindowSizesWindowSizes);
+        }
+
+        // Get shared preferences
+        StringBuilder preferences = new StringBuilder();
+
+        try {
+            File file = new File(context.getFilesDir().getParent() + "/shared_prefs/" + context.getPackageName() + "_preferences.xml");
+            FileInputStream input = new FileInputStream(file);
+            InputStreamReader reader = new InputStreamReader(input);
+            BufferedReader buffer = new BufferedReader(reader);
+
+            String line = buffer.readLine();
+            while(line != null) {
+                preferences.append(line);
+                line = buffer.readLine();
+                if(line != null)
+                    preferences.append("\n");
+            }
+
+            reader.close();
+        } catch (IOException e) { /* Gracefully fail */ }
+
+        agent.putString("preferences", preferences.toString());
+    }
+
+    public static void restore(Context context, BackupAgent agent) {
+        // Get pinned and blocked apps
+        PinnedBlockedApps pba = PinnedBlockedApps.getInstance(context);
+        pba.clear(context);
+
+        String[] pinnedAppsPackageNames = agent.getStringArray("pinned_apps_package_names");
+        String[] pinnedAppsComponentNames = agent.getStringArray("pinned_apps_component_names");
+        String[] pinnedAppsLabels = agent.getStringArray("pinned_apps_labels");
+        long[] pinnedAppsUserIds = agent.getLongArray("pinned_apps_user_ids");
+
+        UserManager userManager =(UserManager) context.getSystemService(Context.USER_SERVICE);
+        LauncherApps launcherApps =(LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
+
+        if(pinnedAppsPackageNames != null && pinnedAppsComponentNames != null && pinnedAppsLabels != null)
+            for(int i = 0; i < pinnedAppsPackageNames.length; i++) {
+                Intent throwaway = new Intent();
+                throwaway.setComponent(ComponentName.unflattenFromString(pinnedAppsComponentNames[i]));
+
+                long userId;
+                if(pinnedAppsUserIds != null)
+                    userId = pinnedAppsUserIds[i];
+                else
+                    userId = userManager.getSerialNumberForUser(Process.myUserHandle());
+
+                AppEntry newEntry = new AppEntry(
+                        pinnedAppsPackageNames[i],
+                        pinnedAppsComponentNames[i],
+                        pinnedAppsLabels[i],
+                        IconCache.getInstance(context).getIcon(
+                                context,
+                                launcherApps.resolveActivity(throwaway, userManager.getUserForSerialNumber(userId))),
+                        true
+                );
+
+                newEntry.setUserId(userId);
+                pba.addPinnedApp(context, newEntry);
+            }
+
+        String[] blockedAppsPackageNames = agent.getStringArray("blocked_apps_package_names");
+        String[] blockedAppsComponentNames = agent.getStringArray("blocked_apps_component_names");
+        String[] blockedAppsLabels = agent.getStringArray("blocked_apps_labels");
+
+        if(blockedAppsPackageNames != null && blockedAppsComponentNames != null && blockedAppsLabels != null)
+            for(int i = 0; i < blockedAppsPackageNames.length; i++) {
+                pba.addBlockedApp(context, new AppEntry(
+                        blockedAppsPackageNames[i],
+                        blockedAppsComponentNames[i],
+                        blockedAppsLabels[i],
+                        null,
+                        false
+                ));
+            }
+
+        // Get blacklist
+        Blacklist blacklist = Blacklist.getInstance(context);
+        blacklist.clear(context);
+
+        String[] blacklistPackageNames = agent.getStringArray("blacklist_package_names");
+        String[] blacklistLabels = agent.getStringArray("blacklist_labels");
+
+        if(blacklistPackageNames != null && blacklistLabels != null)
+            for(int i = 0; i < blacklistPackageNames.length; i++) {
+                blacklist.addBlockedApp(context, new BlacklistEntry(
+                        blacklistPackageNames[i],
+                        blacklistLabels[i]
+                ));
+            }
+
+        // Get top apps
+        TopApps topApps = TopApps.getInstance(context);
+        topApps.clear(context);
+
+        String[] topAppsPackageNames = agent.getStringArray("top_apps_package_names");
+        String[] topAppsLabels = agent.getStringArray("top_apps_labels");
+
+        if(topAppsPackageNames != null && topAppsLabels != null)
+            for(int i = 0; i < topAppsPackageNames.length; i++) {
+                topApps.addTopApp(context, new BlacklistEntry(
+                        topAppsPackageNames[i],
+                        topAppsLabels[i]
+                ));
+            }
+
+        // Get saved window sizes
+        if(U.canEnableFreeform()) {
+            SavedWindowSizes savedWindowSizes = SavedWindowSizes.getInstance(context);
+            savedWindowSizes.clear(context);
+
+            String[] savedWindowSizesComponentNames = agent.getStringArray("saved_window_sizes_component_names");
+            String[] savedWindowSizesWindowSizes = agent.getStringArray("saved_window_sizes_window_sizes");
+
+            if(savedWindowSizesComponentNames != null && savedWindowSizesWindowSizes != null)
+                for(int i = 0; i < savedWindowSizesComponentNames.length; i++) {
+                    savedWindowSizes.setWindowSize(context,
+                            savedWindowSizesComponentNames[i],
+                            savedWindowSizesWindowSizes[i]
+                    );
+                }
+        }
+
+        // Get shared preferences
+        String contents = agent.getString("preferences");
+        if(contents.length() > 0)
+            try {
+                File file = new File(context.getFilesDir().getParent() + "/shared_prefs/" + context.getPackageName() + "_preferences.xml");
+                FileOutputStream output = new FileOutputStream(file);
+                output.write(contents.getBytes());
+                output.close();
+            } catch (IOException e) { /* Gracefully fail */ }
+    }
+}
\ No newline at end of file
diff --git a/app/src/main/java/com/farmerbb/taskbar/backup/IntentBackupAgent.java b/app/src/main/java/com/farmerbb/taskbar/backup/IntentBackupAgent.java
new file mode 100644 (file)
index 0000000..b60c340
--- /dev/null
@@ -0,0 +1,57 @@
+/* Copyright 2020 Braden Farmer
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.farmerbb.taskbar.backup;
+
+import android.content.Intent;
+
+public class IntentBackupAgent implements BackupAgent {
+
+    private Intent intent;
+
+    public IntentBackupAgent(Intent intent) {
+        this.intent = intent;
+    }
+
+    @Override
+    public void putString(String key, String value) {
+        intent.putExtra(key, value);
+    }
+
+    @Override
+    public void putStringArray(String key, String[] value) {
+        intent.putExtra(key, value);
+    }
+
+    @Override
+    public void putLongArray(String key, long[] value) {
+        intent.putExtra(key, value);
+    }
+
+    @Override
+    public String getString(String key) {
+        return intent.getStringExtra(key);
+    }
+
+    @Override
+    public String[] getStringArray(String key) {
+        return intent.getStringArrayExtra(key);
+    }
+
+    @Override
+    public long[] getLongArray(String key) {
+        return intent.getLongArrayExtra(key);
+    }
+}
\ No newline at end of file
diff --git a/app/src/main/java/com/farmerbb/taskbar/backup/JSONBackupAgent.java b/app/src/main/java/com/farmerbb/taskbar/backup/JSONBackupAgent.java
new file mode 100644 (file)
index 0000000..1dac22f
--- /dev/null
@@ -0,0 +1,103 @@
+/* Copyright 2020 Braden Farmer
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.farmerbb.taskbar.backup;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class JSONBackupAgent implements BackupAgent {
+
+    private JSONObject json;
+
+    public JSONBackupAgent(JSONObject json) {
+        this.json = json;
+    }
+
+    @Override
+    public void putString(String key, String value) {
+        try {
+            json.put(key, value);
+        } catch (JSONException e) { /* Gracefully fail */ }
+    }
+
+    @Override
+    public void putStringArray(String key, String[] value) {
+        try {
+            JSONArray array = new JSONArray();
+
+            for(String v : value) {
+                array.put(v);
+            }
+
+            json.put(key, array);
+        } catch (JSONException e) { /* Gracefully fail */ }
+    }
+
+    @Override
+    public void putLongArray(String key, long[] value) {
+        try {
+            JSONArray array = new JSONArray();
+
+            for(long v : value) {
+                array.put(v);
+            }
+
+            json.put(key, array);
+        } catch (JSONException e) { /* Gracefully fail */ }
+    }
+
+    @Override
+    public String getString(String key) {
+        try {
+            return json.getString(key);
+        } catch (JSONException e) {
+            return null;
+        }
+    }
+
+    @Override
+    public String[] getStringArray(String key) {
+        try {
+            JSONArray array = json.getJSONArray(key);
+            String[] returnValue = new String[array.length()];
+
+            for(int i = 0; i < array.length(); i++) {
+                returnValue[i] = array.getString(i);
+            }
+
+            return returnValue;
+        } catch (JSONException e) {
+            return null;
+        }
+    }
+
+    @Override
+    public long[] getLongArray(String key) {
+        try {
+            JSONArray array = json.getJSONArray(key);
+            long[] returnValue = new long[array.length()];
+
+            for(int i = 0; i < array.length(); i++) {
+                returnValue[i] = array.getLong(i);
+            }
+
+            return returnValue;
+        } catch (JSONException e) {
+            return null;
+        }
+    }
+}
\ No newline at end of file
index 3015f97..ed40ae1 100644 (file)
 package com.farmerbb.taskbar.receiver;
 
 import android.content.BroadcastReceiver;
-import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
-import android.content.pm.LauncherApps;
 import android.net.Uri;
-import android.os.Process;
-import android.os.UserManager;
-import androidx.core.content.FileProvider;
 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
 
 import com.farmerbb.taskbar.BuildConfig;
-import com.farmerbb.taskbar.util.AppEntry;
-import com.farmerbb.taskbar.util.Blacklist;
-import com.farmerbb.taskbar.util.BlacklistEntry;
-import com.farmerbb.taskbar.util.IconCache;
-import com.farmerbb.taskbar.util.PinnedBlockedApps;
-import com.farmerbb.taskbar.util.SavedWindowSizes;
-import com.farmerbb.taskbar.util.TopApps;
+import com.farmerbb.taskbar.backup.BackupUtils;
+import com.farmerbb.taskbar.backup.IntentBackupAgent;
 import com.farmerbb.taskbar.util.U;
 
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 
 public class ReceiveSettingsReceiver extends BroadcastReceiver {
@@ -45,114 +34,7 @@ public class ReceiveSettingsReceiver extends BroadcastReceiver {
     public void onReceive(Context context, Intent intent) {
         // Ignore this broadcast if this is the free version
         if(context.getPackageName().equals(BuildConfig.PAID_APPLICATION_ID)) {
-            // Get pinned and blocked apps
-            PinnedBlockedApps pba = PinnedBlockedApps.getInstance(context);
-            pba.clear(context);
-
-            String[] pinnedAppsPackageNames = intent.getStringArrayExtra("pinned_apps_package_names");
-            String[] pinnedAppsComponentNames = intent.getStringArrayExtra("pinned_apps_component_names");
-            String[] pinnedAppsLabels = intent.getStringArrayExtra("pinned_apps_labels");
-            long[] pinnedAppsUserIds = intent.getLongArrayExtra("pinned_apps_user_ids");
-
-            UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
-            LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
-
-            if(pinnedAppsPackageNames != null && pinnedAppsComponentNames != null && pinnedAppsLabels != null)
-                for(int i = 0; i < pinnedAppsPackageNames.length; i++) {
-                    Intent throwaway = new Intent();
-                    throwaway.setComponent(ComponentName.unflattenFromString(pinnedAppsComponentNames[i]));
-
-                    long userId;
-                    if(pinnedAppsUserIds != null)
-                        userId = pinnedAppsUserIds[i];
-                    else
-                        userId = userManager.getSerialNumberForUser(Process.myUserHandle());
-
-                    AppEntry newEntry = new AppEntry(
-                            pinnedAppsPackageNames[i],
-                            pinnedAppsComponentNames[i],
-                            pinnedAppsLabels[i],
-                            IconCache.getInstance(context).getIcon(
-                                    context,
-                                    launcherApps.resolveActivity(throwaway, userManager.getUserForSerialNumber(userId))),
-                            true
-                    );
-
-                    newEntry.setUserId(userId);
-                    pba.addPinnedApp(context, newEntry);
-                }
-
-            String[] blockedAppsPackageNames = intent.getStringArrayExtra("blocked_apps_package_names");
-            String[] blockedAppsComponentNames = intent.getStringArrayExtra("blocked_apps_component_names");
-            String[] blockedAppsLabels = intent.getStringArrayExtra("blocked_apps_labels");
-
-            if(blockedAppsPackageNames != null && blockedAppsComponentNames != null && blockedAppsLabels != null)
-                for(int i = 0; i < blockedAppsPackageNames.length; i++) {
-                    pba.addBlockedApp(context, new AppEntry(
-                            blockedAppsPackageNames[i],
-                            blockedAppsComponentNames[i],
-                            blockedAppsLabels[i],
-                            null,
-                            false
-                    ));
-                }
-
-            // Get blacklist
-            Blacklist blacklist = Blacklist.getInstance(context);
-            blacklist.clear(context);
-
-            String[] blacklistPackageNames = intent.getStringArrayExtra("blacklist_package_names");
-            String[] blacklistLabels = intent.getStringArrayExtra("blacklist_labels");
-
-            if(blacklistPackageNames != null && blacklistLabels != null)
-                for(int i = 0; i < blacklistPackageNames.length; i++) {
-                    blacklist.addBlockedApp(context, new BlacklistEntry(
-                            blacklistPackageNames[i],
-                            blacklistLabels[i]
-                    ));
-                }
-
-            // Get top apps
-            TopApps topApps = TopApps.getInstance(context);
-            topApps.clear(context);
-
-            String[] topAppsPackageNames = intent.getStringArrayExtra("top_apps_package_names");
-            String[] topAppsLabels = intent.getStringArrayExtra("top_apps_labels");
-
-            if(topAppsPackageNames != null && topAppsLabels != null)
-                for(int i = 0; i < topAppsPackageNames.length; i++) {
-                    topApps.addTopApp(context, new BlacklistEntry(
-                            topAppsPackageNames[i],
-                            topAppsLabels[i]
-                    ));
-                }
-
-            // Get saved window sizes
-            if(U.canEnableFreeform()) {
-                SavedWindowSizes savedWindowSizes = SavedWindowSizes.getInstance(context);
-                savedWindowSizes.clear(context);
-
-                String[] savedWindowSizesComponentNames = intent.getStringArrayExtra("saved_window_sizes_component_names");
-                String[] savedWindowSizesWindowSizes = intent.getStringArrayExtra("saved_window_sizes_window_sizes");
-
-                if(savedWindowSizesComponentNames != null && savedWindowSizesWindowSizes != null)
-                    for(int i = 0; i < savedWindowSizesComponentNames.length; i++) {
-                        savedWindowSizes.setWindowSize(context,
-                                savedWindowSizesComponentNames[i],
-                                savedWindowSizesWindowSizes[i]
-                        );
-                    }
-            }
-
-            // Get shared preferences
-            String contents = intent.getStringExtra("preferences");
-            if(contents.length() > 0)
-                try {
-                    File file = new File(context.getFilesDir().getParent() + "/shared_prefs/" + context.getPackageName() + "_preferences.xml");
-                    FileOutputStream output = new FileOutputStream(file);
-                    output.write(contents.getBytes());
-                    output.close();
-                } catch (IOException e) { /* Gracefully fail */ }
+            BackupUtils.restore(context, new IntentBackupAgent(intent));
 
             // Get custom start button image
             if(intent.hasExtra("custom_image")) {
index 4faae06..0b50632 100644 (file)
@@ -22,21 +22,11 @@ import android.net.Uri;
 import androidx.core.content.FileProvider;
 
 import com.farmerbb.taskbar.BuildConfig;
-import com.farmerbb.taskbar.util.AppEntry;
-import com.farmerbb.taskbar.util.Blacklist;
-import com.farmerbb.taskbar.util.BlacklistEntry;
-import com.farmerbb.taskbar.util.PinnedBlockedApps;
-import com.farmerbb.taskbar.util.SavedWindowSizes;
-import com.farmerbb.taskbar.util.SavedWindowSizesEntry;
-import com.farmerbb.taskbar.util.TopApps;
+import com.farmerbb.taskbar.backup.BackupUtils;
+import com.farmerbb.taskbar.backup.IntentBackupAgent;
 import com.farmerbb.taskbar.util.U;
 
-import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.util.List;
 
 public class SendSettingsReceiver extends BroadcastReceiver {
     @Override
@@ -46,116 +36,7 @@ public class SendSettingsReceiver extends BroadcastReceiver {
             Intent sendSettingsIntent = new Intent("com.farmerbb.taskbar.SEND_SETTINGS");
             sendSettingsIntent.setPackage(BuildConfig.PAID_APPLICATION_ID);
 
-            // Get pinned and blocked apps
-            PinnedBlockedApps pba = PinnedBlockedApps.getInstance(context);
-            List<AppEntry> pinnedAppsList = pba.getPinnedApps();
-
-            String[] pinnedAppsPackageNames = new String[pinnedAppsList.size()];
-            String[] pinnedAppsComponentNames = new String[pinnedAppsList.size()];
-            String[] pinnedAppsLabels = new String[pinnedAppsList.size()];
-            long[] pinnedAppsUserIds = new long[pinnedAppsList.size()];
-
-            for(int i = 0; i < pinnedAppsList.size(); i++) {
-                AppEntry entry = pinnedAppsList.get(i);
-                pinnedAppsPackageNames[i] = entry.getPackageName();
-                pinnedAppsComponentNames[i] = entry.getComponentName();
-                pinnedAppsLabels[i] = entry.getLabel();
-                pinnedAppsUserIds[i] = entry.getUserId(context);
-            }
-
-            sendSettingsIntent.putExtra("pinned_apps_package_names", pinnedAppsPackageNames);
-            sendSettingsIntent.putExtra("pinned_apps_component_names", pinnedAppsComponentNames);
-            sendSettingsIntent.putExtra("pinned_apps_labels", pinnedAppsLabels);
-            sendSettingsIntent.putExtra("pinned_apps_user_ids", pinnedAppsUserIds);
-
-            List<AppEntry> blockedAppsList = pba.getBlockedApps();
-
-            String[] blockedAppsPackageNames = new String[blockedAppsList.size()];
-            String[] blockedAppsComponentNames = new String[blockedAppsList.size()];
-            String[] blockedAppsLabels = new String[blockedAppsList.size()];
-
-            for(int i = 0; i < blockedAppsList.size(); i++) {
-                AppEntry entry = blockedAppsList.get(i);
-                blockedAppsPackageNames[i] = entry.getPackageName();
-                blockedAppsComponentNames[i] = entry.getComponentName();
-                blockedAppsLabels[i] = entry.getLabel();
-            }
-
-            sendSettingsIntent.putExtra("blocked_apps_package_names", blockedAppsPackageNames);
-            sendSettingsIntent.putExtra("blocked_apps_component_names", blockedAppsComponentNames);
-            sendSettingsIntent.putExtra("blocked_apps_labels", blockedAppsLabels);
-
-            // Get blacklist
-            Blacklist blacklist = Blacklist.getInstance(context);
-            List<BlacklistEntry> blacklistList = blacklist.getBlockedApps();
-
-            String[] blacklistPackageNames = new String[blacklistList.size()];
-            String[] blacklistLabels = new String[blacklistList.size()];
-
-            for(int i = 0; i < blacklistList.size(); i++) {
-                BlacklistEntry entry = blacklistList.get(i);
-                blacklistPackageNames[i] = entry.getPackageName();
-                blacklistLabels[i] = entry.getLabel();
-            }
-
-            sendSettingsIntent.putExtra("blacklist_package_names", blacklistPackageNames);
-            sendSettingsIntent.putExtra("blacklist_labels", blacklistLabels);
-
-            // Get top apps
-            TopApps topApps = TopApps.getInstance(context);
-            List<BlacklistEntry> topAppsList = topApps.getTopApps();
-
-            String[] topAppsPackageNames = new String[topAppsList.size()];
-            String[] topAppsLabels = new String[topAppsList.size()];
-
-            for(int i = 0; i < topAppsList.size(); i++) {
-                BlacklistEntry entry = topAppsList.get(i);
-                topAppsPackageNames[i] = entry.getPackageName();
-                topAppsLabels[i] = entry.getLabel();
-            }
-
-            sendSettingsIntent.putExtra("top_apps_package_names", topAppsPackageNames);
-            sendSettingsIntent.putExtra("top_apps_labels", topAppsLabels);
-
-            // Get saved window sizes
-            if(U.canEnableFreeform()) {
-                SavedWindowSizes savedWindowSizes = SavedWindowSizes.getInstance(context);
-                List<SavedWindowSizesEntry> savedWindowSizesList = savedWindowSizes.getSavedWindowSizes();
-
-                String[] savedWindowSizesComponentNames = new String[savedWindowSizesList.size()];
-                String[] savedWindowSizesWindowSizes = new String[savedWindowSizesList.size()];
-
-                for(int i = 0; i < savedWindowSizesList.size(); i++) {
-                    SavedWindowSizesEntry entry = savedWindowSizesList.get(i);
-                    savedWindowSizesComponentNames[i] = entry.getComponentName();
-                    savedWindowSizesWindowSizes[i] = entry.getWindowSize();
-                }
-
-                sendSettingsIntent.putExtra("saved_window_sizes_component_names", savedWindowSizesComponentNames);
-                sendSettingsIntent.putExtra("saved_window_sizes_window_sizes", savedWindowSizesWindowSizes);
-            }
-
-            // Get shared preferences
-            StringBuilder preferences = new StringBuilder();
-
-            try {
-                File file = new File(context.getFilesDir().getParent() + "/shared_prefs/" + context.getPackageName() + "_preferences.xml");
-                FileInputStream input = new FileInputStream(file);
-                InputStreamReader reader = new InputStreamReader(input);
-                BufferedReader buffer = new BufferedReader(reader);
-
-                String line = buffer.readLine();
-                while(line != null) {
-                    preferences.append(line);
-                    line = buffer.readLine();
-                    if(line != null)
-                        preferences.append("\n");
-                }
-
-                reader.close();
-            } catch (IOException e) { /* Gracefully fail */ }
-
-            sendSettingsIntent.putExtra("preferences", preferences.toString());
+            BackupUtils.backup(context, new IntentBackupAgent(sendSettingsIntent));
 
             // Get custom start button image
             File file = new File(context.getFilesDir() + "/tb_images", "custom_image");