[Android] [PATCH 1/8] extract database access to AsyncTask

Yu Lin yu.lin.86 at gmail.com
Tue Mar 4 23:33:39 CET 2014


---
 .../src/org/videolan/vlc/gui/BrowserAdapter.java   |   64 +++++++++++++------
 1 files changed, 44 insertions(+), 20 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/BrowserAdapter.java b/vlc-android/src/org/videolan/vlc/gui/BrowserAdapter.java
index 1aeb497..c6b805c 100644
--- a/vlc-android/src/org/videolan/vlc/gui/BrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/BrowserAdapter.java
@@ -29,6 +29,7 @@ import org.videolan.vlc.R;
 import org.videolan.vlc.VLCApplication;
 
 import android.content.Context;
+import android.os.AsyncTask;
 import android.os.Build;
 import android.os.Environment;
 import android.util.Log;
@@ -89,21 +90,32 @@ public class BrowserAdapter extends ArrayAdapter<File>
             holder.check.setEnabled(true);
             holder.check.setChecked(false);
 
-            List<File> dirs = dbManager.getMediaDirs();
-            for (File dir : dirs) {
-                if (dir.getPath().equals(item.getPath())) {
-                    holder.check.setEnabled(true);
-                    holder.check.setChecked(true);
-                    break;
-                } else if (dir.getPath().startsWith(item.getPath()+"/")) {
-                    Log.i(TAG, dir.getPath() + " startWith " + item.getPath());
-                    holder.check.setEnabled(false);
-                    holder.check.setChecked(true);
-                    break;
+            new AsyncTask<Object, Void, List<File>>() {
+                ViewHolder holder;
+                protected List<File> doInBackground(Object... args) {
+                    MediaDatabase dbManager = (MediaDatabase) args[0];
+                    this.holder = (ViewHolder) args[1];
+                    List<File> dirs = dbManager.getMediaDirs();
+                    return dirs;
                 }
-            }
 
-            holder.check.setOnCheckedChangeListener(onCheckedChangeListener);
+                protected void onPostExecute(List<File> dirs) {
+                    for (File dir : dirs) {
+                        if (dir.getPath().equals(item.getPath())) {
+                            holder.check.setEnabled(true);
+                            holder.check.setChecked(true);
+                            break;
+                        } else if (dir.getPath().startsWith(item.getPath()+"/")) {
+                            Log.i(TAG, dir.getPath() + " startWith " + item.getPath());
+                            holder.check.setEnabled(false);
+                            holder.check.setChecked(true);
+                            break;
+                        }
+                    }
+
+                    holder.check.setOnCheckedChangeListener(onCheckedChangeListener);
+                }
+            }.execute(dbManager, holder);
         }
 
         return view;
@@ -118,14 +130,26 @@ public class BrowserAdapter extends ArrayAdapter<File>
                 return;
 
             if (buttonView.isEnabled() && isChecked) {
-                dbManager.addDir(item.getPath());
-                File tmpFile = item.getParentFile();
-                while (tmpFile != null && !tmpFile.getPath().equals("/")) {
-                    dbManager.removeDir(tmpFile.getPath());
-                    tmpFile = tmpFile.getParentFile();
-                }
+                new AsyncTask<File, Void, Void>() {
+                    protected Void doInBackground(File... args) {
+                        File item = args[0];
+                        dbManager.addDir(item.getPath());
+                        File tmpFile = item.getParentFile();
+                        while (tmpFile != null && !tmpFile.getPath().equals("/")) {
+                            dbManager.removeDir(tmpFile.getPath());
+                            tmpFile = tmpFile.getParentFile();
+                        }
+                        return null;
+                    }
+                }.execute(item);
             } else {
-                dbManager.removeDir(item.getPath());
+                new AsyncTask<File, Void, Void>() {
+                    protected Void doInBackground(File... args) {
+                        File item = args[0];
+                        dbManager.removeDir(item.getPath());
+                        return null;
+                    }
+                }.execute(item);
             }
         }
     };
-- 
1.7.4.4



More information about the Android mailing list