Explorar o código

Filelisting using intents and OCFile.getId() instead of string matching
in db which can be very very slow if there are lots of files

Lennart Rosam %!s(int64=13) %!d(string=hai) anos
pai
achega
e8c9e5b09a

+ 1 - 0
res/values/strings.xml

@@ -54,6 +54,7 @@
     <string name="filedetails_created">Created:</string>
     <string name="filedetails_modified">Modified:</string>
     <string name="filedetails_download">Download</string>
+    <string name="filedetails_open">Open</string>
     <string name="common_yes">Yes</string>
     <string name="common_no">No</string>
     <string name="common_ok">OK</string>

+ 25 - 3
src/eu/alefzero/owncloud/ui/activity/FileDetailActivity.java

@@ -17,11 +17,13 @@
  */
 package eu.alefzero.owncloud.ui.activity;
 
+import android.content.Intent;
 import android.os.Bundle;
 import android.support.v4.app.FragmentTransaction;
-import android.view.Window;
 
+import com.actionbarsherlock.app.ActionBar;
 import com.actionbarsherlock.app.SherlockFragmentActivity;
+import com.actionbarsherlock.view.MenuItem;
 
 import eu.alefzero.owncloud.R;
 import eu.alefzero.owncloud.ui.fragment.FileDetailFragment;
@@ -38,11 +40,12 @@ public class FileDetailActivity extends SherlockFragmentActivity {
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
-        // TODO Auto-generated method stub
         super.onCreate(savedInstanceState);
-        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
         setContentView(R.layout.file_activity_details);
 
+        ActionBar actionBar = getSupportActionBar();
+        actionBar.setDisplayHomeAsUpEnabled(true);
+        
         FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
         mFileDetail = new FileDetailFragment();
         ft.add(R.id.fragment, mFileDetail, "FileDetails");
@@ -50,6 +53,25 @@ public class FileDetailActivity extends SherlockFragmentActivity {
 
     }
 
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        boolean returnValue = false;
+        
+        switch(item.getItemId()){
+        case android.R.id.home:
+            Intent intent = new Intent(this, FileDisplayActivity.class);
+            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+            intent.putExtra(FileDetailFragment.EXTRA_FILE, mFileDetail.getDisplayedFile());
+            startActivity(intent);
+            finish();
+            returnValue = true;
+        }
+        
+        return returnValue;
+    }
+
+
+
     @Override
     protected void onResume() {
         super.onResume();

+ 3 - 9
src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java

@@ -20,8 +20,6 @@ package eu.alefzero.owncloud.ui.activity;
 
 import java.io.File;
 import java.net.URLDecoder;
-import java.net.URLEncoder;
-
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.app.AlertDialog;
@@ -62,7 +60,6 @@ import eu.alefzero.owncloud.datamodel.OCFile;
 import eu.alefzero.owncloud.files.services.FileUploader;
 import eu.alefzero.owncloud.syncadapter.FileSyncService;
 import eu.alefzero.owncloud.ui.fragment.FileListFragment;
-import eu.alefzero.owncloud.utils.OwnCloudVersion;
 import eu.alefzero.webdav.WebdavClient;
 
 /**
@@ -84,8 +81,6 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
     
     private static final int DIALOG_SETUP_ACCOUNT = 0;
     private static final int DIALOG_CREATE_DIR = 1;
-
-    private static final int REQUEST_ACCOUNT_SETUP = 0;
     private static final int ACTION_SELECT_FILE = 1;
 
     public void pushPath(String path) {
@@ -175,8 +170,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
 
         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
         setProgressBarIndeterminateVisibility(false);
-        // if (getSupportFragmentManager().findFragmentById(R.id.fileList) ==
-        // null)
+        
         setContentView(R.layout.files);
 
     }
@@ -282,7 +276,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 mDirectories.insert(s, 0);
             FileListFragment fileListFramgent = (FileListFragment) getSupportFragmentManager()
                     .findFragmentById(R.id.fileList);
-            if (fileListFramgent != null) fileListFramgent.populateFileList();
+            if (fileListFramgent != null) fileListFramgent.listDirectory();
         }
 
         mStorageManager = new FileDataStorageManager(
@@ -466,7 +460,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 FileListFragment fileListFramgent = (FileListFragment) getSupportFragmentManager()
                         .findFragmentById(R.id.fileList);
                 if (fileListFramgent != null)
-                    fileListFramgent.populateFileList();
+                    fileListFramgent.listDirectory();
             }
         }
 

+ 71 - 38
src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java

@@ -51,7 +51,7 @@ import eu.alefzero.owncloud.datamodel.OCFile;
 public class FileDetailFragment extends SherlockFragment implements
         OnClickListener {
 
-    public static final String FILE = "FILE";
+    public static final String EXTRA_FILE = "FILE";
 
     private DownloadFinishReceiver mDownloadFinishReceiver;
     private Intent mIntent;
@@ -109,6 +109,46 @@ public class FileDetailFragment extends SherlockFragment implements
         mDownloadFinishReceiver = null;
     }
 
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+            Bundle savedInstanceState) {
+        View view = null;
+        view = inflater.inflate(mLayout, container, false);
+        mView = view;
+        if(mLayout == R.layout.file_details_fragment){
+            // Phones will launch an activity with this intent
+            if(mIntent == null){
+                mIntent = getActivity().getIntent();
+            }
+            updateFileDetails();
+        }
+        
+        return view;
+    }
+
+    @Override
+    public View getView() {
+        return super.getView() == null ? mView : super.getView();
+    }
+
+    @Override
+    public void onClick(View v) {
+        Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();
+        Intent i = new Intent(getActivity(), FileDownloader.class);
+        i.putExtra(FileDownloader.EXTRA_ACCOUNT,
+                mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT));
+        i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getPath());
+        getActivity().startService(i);
+    }
+
+    /**
+     * Can be used to get the file that is currently being displayed.
+     * @return The file on the screen.
+     */
+    public OCFile getDisplayedFile(){
+        return mFile;
+    }
+    
     /**
      * Use this method to signal this Activity that it shall update its view.
      * 
@@ -116,7 +156,7 @@ public class FileDetailFragment extends SherlockFragment implements
      *            this file The intent needs to have these extras:
      *            <p>
      * 
-     *            {@link FileDetailFragment#FILE}: An {@link OCFile}
+     *            {@link FileDetailFragment#EXTRA_FILE}: An {@link OCFile}
      *            {@link FileDownloader#EXTRA_ACCOUNT}: The Account that file
      *            belongs to (required for downloading)
      */
@@ -125,8 +165,11 @@ public class FileDetailFragment extends SherlockFragment implements
         updateFileDetails();
     }
 
+    /**
+     * Updates the view with all relevant details about that file.
+     */
     private void updateFileDetails() {
-        mFile = mIntent.getParcelableExtra(FILE);
+        mFile = mIntent.getParcelableExtra(EXTRA_FILE);
         Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);
 
         if (mFile != null) {
@@ -150,7 +193,7 @@ public class FileDetailFragment extends SherlockFragment implements
                 } catch (OutOfMemoryError e) {
                     Log.e(TAG, "Out of memory occured for file with size " + mFile.getFileLength());
                 }
-                downloadButton.setText("Open file");
+                downloadButton.setText(R.string.filedetails_open);
                 downloadButton.setOnClickListener(new OnClickListener() {
                     @Override
                     public void onClick(View v) {
@@ -166,24 +209,40 @@ public class FileDetailFragment extends SherlockFragment implements
         }
     }
     
+    /**
+     * Updates the filename in view
+     * @param filename to set
+     */
     private void setFilename(String filename) {
         TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
         if (tv != null)
             tv.setText(filename);
     }
 
+    /**
+     * Updates the MIME type in view
+     * @param mimetype to set
+     */
     private void setFiletype(String mimetype) {
         TextView tv = (TextView) getView().findViewById(R.id.fdType);
         if (tv != null)
             tv.setText(mimetype);
     }
 
+    /**
+     * Updates the file size in view
+     * @param filesize in bytes to set
+     */
     private void setFilesize(long filesize) {
         TextView tv = (TextView) getView().findViewById(R.id.fdSize);
         if (tv != null)
             tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
     }
     
+    /**
+     * Updates the time that the file was created in view
+     * @param milliseconds Unix time to set
+     */
     private void setTimeCreated(long milliseconds){
         TextView tv = (TextView) getView().findViewById(R.id.fdCreated);
         if(tv != null){
@@ -191,6 +250,10 @@ public class FileDetailFragment extends SherlockFragment implements
         }
     }
     
+    /**
+     * Updates the time that the file was last modified
+     * @param milliseconds Unix time to set
+     */
     private void setTimeModified(long milliseconds){
         TextView tv = (TextView) getView().findViewById(R.id.fdModified);
         if(tv != null){
@@ -198,40 +261,10 @@ public class FileDetailFragment extends SherlockFragment implements
         }
     }
 
-    @Override
-    public View onCreateView(LayoutInflater inflater, ViewGroup container,
-            Bundle savedInstanceState) {
-        View view = null;
-        view = inflater.inflate(mLayout, container, false);
-        mView = view;
-        if(mLayout == R.layout.file_details_fragment){
-            // Phones will launch an activity with this intent
-            if(mIntent == null){
-                mIntent = getActivity().getIntent();
-            }
-            updateFileDetails();
-        }
-        
-        return view;
-    }
-    
-    
-
-    @Override
-    public View getView() {
-        return super.getView() == null ? mView : super.getView();
-    }
-
-    @Override
-    public void onClick(View v) {
-        Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();
-        Intent i = new Intent(getActivity(), FileDownloader.class);
-        i.putExtra(FileDownloader.EXTRA_ACCOUNT,
-                mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT));
-        i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getPath());
-        getActivity().startService(i);
-    }
-
+    /**
+     * Once the file download has finished -> update view
+     * @author Bartek Przybylski
+     */
     private class DownloadFinishReceiver extends BroadcastReceiver {
         @Override
         public void onReceive(Context context, Intent intent) {

+ 52 - 61
src/eu/alefzero/owncloud/ui/fragment/FileListFragment.java

@@ -48,10 +48,12 @@ import eu.alefzero.owncloud.ui.adapter.FileListListAdapter;
  * 
  */
 public class FileListFragment extends FragmentListView {
+    private static final String TAG = "FileListFragment";
     private Account mAccount;
     private Stack<String> mDirNames;
     private Vector<OCFile> mFiles;
     private DataStorageManager mStorageManager;
+    private OCFile mFile;
     private boolean mIsLargeDevice = false;
 
     public FileListFragment() {
@@ -63,15 +65,19 @@ public class FileListFragment extends FragmentListView {
         super.onCreate(savedInstanceState);
 
         mAccount = AccountUtils.getCurrentOwnCloudAccount(getActivity());
+        mStorageManager = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
         getListView().setDivider(getResources().getDrawable(R.drawable.uploader_list_separator));
         getListView().setDividerHeight(1);
 
-        populateFileList();
+        Intent intent = getActivity().getIntent();
+        OCFile directory = intent.getParcelableExtra(FileDetailFragment.EXTRA_FILE);
+        mFile = directory;
+        
+        listDirectory(directory);
     }
 
     @Override
     public void onStart() {
-                
         // Create a placeholder upon launch
         View fragmentContainer = getActivity().findViewById(R.id.file_details_container);
         if (fragmentContainer != null) {
@@ -89,22 +95,23 @@ public class FileListFragment extends FragmentListView {
             throw new IndexOutOfBoundsException("Incorrect item selected");
         }
         OCFile file = mFiles.get(position);
-
+        mFile = file;
+        
         // Update ActionBarPath
         if (file.getMimetype().equals("DIR")) {
             String dirname = file.getFileName();
 
             mDirNames.push(dirname);
             ((FileDisplayActivity) getActivity()).pushPath(dirname);
-
-            populateFileList();
+            
+            listDirectory(file);
             resetFileFragment();
 
             return;
         }
 
         Intent showDetailsIntent = new Intent(getActivity(), FileDetailActivity.class);
-        showDetailsIntent.putExtra(FileDetailFragment.FILE, file);
+        showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file);
         showDetailsIntent.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
 
         // If we are on a large device -> update fragment
@@ -151,26 +158,53 @@ public class FileListFragment extends FragmentListView {
      */
     public void onNavigateUp() {
         mDirNames.pop();
-        populateFileList();
+        OCFile parentDir = null;
+        
+        if(mFile != null){
+            parentDir = mStorageManager.getFileById(mFile.getParentId());
+            mFile = parentDir;
+        }
+        
+        listDirectory(parentDir);
         resetFileFragment();
     }
 
     /**
-     * Lists the directory
+     * Calls {@link FileListFragment#listDirectory(OCFile)} with a null parameter
      */
-    public void populateFileList() {
-        String s = "/";
-        for (String a : mDirNames)
-            s += a + "/";
-        Log.e("ASD", s);
-
-        mStorageManager = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
-        OCFile file = mStorageManager.getFileByPath(s);
-        mFiles = mStorageManager.getDirectoryContent(file);
+    public void listDirectory(){
+        listDirectory(null);
+    }
+    
+    /**
+     * Lists the given directory on the view. When the input parameter is null,
+     * it will either refresh the last known directory, or list the root
+     * if there never was a directory.
+     * 
+     * @param directory File to be listed
+     */
+    public void listDirectory(OCFile directory) {
+                
+        // Check input parameters for null
+        if(directory == null){
+            if(mFile != null){
+                directory = mFile;
+            } else {
+                directory = mStorageManager.getFileByPath("/");
+            }
+        }
+        
+        // If that's not a directory -> List its parent
+        if(!directory.isDirectory()){
+            Log.w(TAG, "You see, that is not a directory -> " + directory.toString());
+            directory = mStorageManager.getFileById(directory.getParentId());
+        }
+        
+        mFiles = mStorageManager.getDirectoryContent(directory);
         if (mFiles == null || mFiles.size() == 0) {
             Toast.makeText(getActivity(), "There are no files here", Toast.LENGTH_LONG).show();
         }
-        setListAdapter(new FileListListAdapter(file, mStorageManager, getActivity()));
+        setListAdapter(new FileListListAdapter(directory, mStorageManager, getActivity()));
     }
 
     @Override
@@ -178,48 +212,5 @@ public class FileListFragment extends FragmentListView {
         super.onSaveInstanceState(outState);
         outState.putParcelable("ACCOUNT", mAccount);
     }
-    
-    // TODO: Delete this testing stuff.
-    /*
-     * private void addContact(Account account, String name, String username) {
-     * Log.i("ASD", "Adding contact: " + name);
-     * ArrayList<ContentProviderOperation> operationList = new
-     * ArrayList<ContentProviderOperation>();
-     * 
-     * //Create our RawContact ContentProviderOperation.Builder builder =
-     * ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
-     * builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
-     * builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
-     * builder.withValue(RawContacts.SYNC1, username);
-     * operationList.add(builder.build());
-     * 
-     * //Create a Data record of common type 'StructuredName' for our RawContact
-     * builder =
-     * ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
-     * builder
-     * .withValueBackReference(ContactsContract.CommonDataKinds.StructuredName
-     * .RAW_CONTACT_ID, 0); builder.withValue(ContactsContract.Data.MIMETYPE,
-     * ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
-     * builder
-     * .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
-     * name); operationList.add(builder.build());
-     * 
-     * //Create a Data record of custom type
-     * "vnd.android.cursor.item/vnd.fm.last.android.profile" to display a link
-     * to the Last.fm profile builder =
-     * ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
-     * builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
-     * builder.withValue(ContactsContract.Data.MIMETYPE,
-     * "vnd.android.cursor.item/vnd.owncloud.contact.profile");
-     * builder.withValue(ContactsContract.Data.DATA1, username);
-     * builder.withValue(ContactsContract.Data.DATA2, "Last.fm Profile");
-     * builder.withValue(ContactsContract.Data.DATA3, "View profile");
-     * operationList.add(builder.build());
-     * 
-     * try {
-     * getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY,
-     * operationList); } catch (Exception e) { Log.e("ASD",
-     * "Something went wrong during creation! " + e); e.printStackTrace(); } }
-     */
 
 }