Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DiskUsage/Dashclock fixes #68

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Backbone/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
<meta-data android:name="description"
android:value="@string/dashclock_extension_description" />
<meta-data android:name="settingsActivity"
android:value=".dashclock.DashSettings" />
android:value="me.toolify.backbone.dashclock.DashSettings" />
</service>

<activity android:name=".dashclock.DashSettings"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,8 @@ public void onActionBarItemClick(View view) {
@Subscribe
public void onBookmarkOpen(BookmarkOpenEvent event) {
String path = event.path;
// Check that the bookmark exists
Bookmark b = Bookmarks.getBookmark(getContentResolver(), path);
// Check that the bookmark exists
try {
FileSystemObject fso = CommandHelper.getFileInfo(this, path, null);
if (fso != null) {
Expand All @@ -991,10 +992,12 @@ public void onBookmarkOpen(BookmarkOpenEvent event) {
} else {
// The bookmark not exists, delete the user-defined bookmark
try {
BusProvider.postEvent(new BookmarkDeleteEvent(path));
Bookmark b = Bookmarks.getBookmark(getContentResolver(), path);
Bookmarks.removeBookmark(this, b);
BusProvider.postEvent(new BookmarkRefreshEvent());
if(b.getType() == Bookmark.BOOKMARK_TYPE.USER_DEFINED)
{
BusProvider.postEvent(new BookmarkDeleteEvent(path));
Bookmarks.removeBookmark(this, b);
BusProvider.postEvent(new BookmarkRefreshEvent());
}
} catch (Exception ex) {/**NON BLOCK**/}
}
} catch (Exception e) {
Expand All @@ -1003,7 +1006,8 @@ public void onBookmarkOpen(BookmarkOpenEvent event) {
if (e instanceof NoSuchFileOrDirectory || e instanceof FileNotFoundException) {
// The bookmark not exists, delete the user-defined bookmark
try {
BusProvider.postEvent(new BookmarkDeleteEvent(path));
if(b.getType() == Bookmark.BOOKMARK_TYPE.USER_DEFINED)
BusProvider.postEvent(new BookmarkDeleteEvent(path));
} catch (Exception ex) {/**NON BLOCK**/}
}
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import android.util.Log;

import me.toolify.backbone.BuildConfig;
import me.toolify.backbone.commands.DiskUsageExecutable;
import me.toolify.backbone.console.ExecutionException;
import me.toolify.backbone.console.InsufficientPermissionsException;
Expand All @@ -27,6 +28,7 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;


Expand All @@ -39,7 +41,7 @@ public class DiskUsageCommand extends Program implements DiskUsageExecutable {

private final String mMountsFile;
private final String mSrc;
private final List<DiskUsage> mDisksUsage;
private final Hashtable<String, DiskUsage> mDisksUsage = new Hashtable<String, DiskUsage>();

/**
* Constructor of <code>DiskUsageCommand</code>.
Expand All @@ -60,15 +62,17 @@ public DiskUsageCommand(String mountsFile, String dir) {
super();
this.mMountsFile = mountsFile;
this.mSrc = null;
this.mDisksUsage = new ArrayList<DiskUsage>();
}

/**
* {@inheritDoc}
*/
@Override
public List<DiskUsage> getResult() {
return this.mDisksUsage;
List<DiskUsage> ret = new ArrayList<DiskUsage>();
for(DiskUsage du : mDisksUsage.values())
ret.add(du);
return ret;
}

/**
Expand All @@ -94,11 +98,12 @@ public void execute()

// Get every disk usage
for (int i = 0; i < mp.size(); i++) {
File root = new File(mp.get(i).getMountPoint());
this.mDisksUsage.add(createDiskUsuage(root));
String mpp = mp.get(i).getMountPoint();
File root = new File(mpp);
mDisksUsage.put(mpp, createDiskUsage(root));
}
} else {
this.mDisksUsage.add(createDiskUsuage(new File(this.mSrc)));
mDisksUsage.put(mSrc, createDiskUsage(new File(this.mSrc)));
}

if (isTrace()) {
Expand All @@ -112,12 +117,14 @@ public void execute()
* @param root The root file
* @return DiskUsage The disk usage
*/
private DiskUsage createDiskUsuage(File file) {
private DiskUsage createDiskUsage(File root) {
long total = root.getTotalSpace();
long free = root.getFreeSpace();
DiskUsage du = new DiskUsage(
file.getAbsolutePath(),
file.getTotalSpace(),
file.getTotalSpace() - file.getFreeSpace(),
file.getFreeSpace());
root.getAbsolutePath(),
total,
total - free,
free);
if (isTrace()) {
Log.v(TAG, du.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,19 @@ public boolean onPreferenceClick(Preference preference) {
for(Object storage : StorageHelper.getStorageVolumes(this))
{
final String path = StorageHelper.getStoragePath(storage);
final String desc = StorageHelper.getStorageVolumeDescription(this, storage);
String desc = StorageHelper.getStorageVolumeDescription(this, storage);
final CheckBoxPreference p = new CheckBoxPreference(this);
int storeIcon = DashExtension.getIcon(desc);
if(desc.equals(getString(R.string.external_storage))||
desc.equals(getString(R.string.internal_storage)))
storeIcon = R.drawable.dashclock_sd;
p.setIcon(storeIcon);
boolean useMe = true;
MountPoint mp = MountPointHelper.getMountPointFromDirectory(path);
if(mp.getOptions().indexOf("mode=0")>-1) useMe = false; // Cyanogenmod USB workaround
if(!MountPointHelper.isReadWrite(mp)) useMe = false;
if(desc.equals(getString(R.string.bookmarks_system_folder)))
desc = path;
p.setTitle(desc);
p.setChecked(prefs.getBoolean("storage_" + path, useMe));
p.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
Expand Down
10 changes: 7 additions & 3 deletions Backbone/src/main/java/me/toolify/backbone/model/Bookmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import android.os.Parcelable;
import android.provider.BaseColumns;

import me.toolify.backbone.providers.BookmarksContentProvider;

import java.io.File;
import java.io.Serializable;

import me.toolify.backbone.providers.BookmarksContentProvider;

/**
* A class that represent a bookmark.
*/
Expand Down Expand Up @@ -190,7 +190,7 @@ public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
if (!(obj instanceof Bookmark)) {
return false;
}
Bookmark other = (Bookmark) obj;
Expand Down Expand Up @@ -268,6 +268,10 @@ public int compareTo(Bookmark another) {
return this.mPath.compareTo(another.mPath);
}

public BOOKMARK_TYPE getType() {
return mType;
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
if (!(obj instanceof MountPoint)) {
return false;
}
MountPoint other = (MountPoint) obj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ protected Boolean doInBackground(String... params) {
if (isCancelled()) {
return Boolean.TRUE;
}
final MountPoint mp = MountPointHelper.getMountPointFromDirectory(dir);
MountPoint mp1 = MountPointHelper.getMountPointFromDirectory(dir);
if(mp1.getMountPoint().equals("/") && dir.equals("/storage/emulated/0")) // AOSP 4.3 bug
mp1 = MountPointHelper.getMountPointFromDirectory("/mnt/shell/emulated");
final MountPoint mp = mp1;
if (mp == null) {
//There is no information about
if (isCancelled()) {
Expand Down
23 changes: 16 additions & 7 deletions Backbone/src/main/java/me/toolify/backbone/util/CommandHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
package me.toolify.backbone.util;

import android.content.Context;
import android.util.Log;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import me.toolify.backbone.BuildConfig;
import me.toolify.backbone.commands.AsyncResultListener;
import me.toolify.backbone.commands.ChangeOwnerExecutable;
import me.toolify.backbone.commands.ChangePermissionsExecutable;
Expand Down Expand Up @@ -74,10 +80,6 @@
import me.toolify.backbone.model.User;
import me.toolify.backbone.preferences.CompressionMode;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;


/**
* A helper class with useful methods for deal with commands.
Expand Down Expand Up @@ -909,9 +911,16 @@ public static DiskUsage getDiskUsage(Context context, String dir, Console consol
DiskUsageExecutable executable =
c.getExecutableFactory().newCreator().createDiskUsageExecutable(dir);
execute(context, executable, c);
List<DiskUsage> du = executable.getResult();
if (du != null && du.size() > 0) {
return du.get(0);
List<DiskUsage> dus = executable.getResult();
if (dus != null && dus.size() > 0) {
for(DiskUsage du : dus)
{
if(!du.getMountPoint().equals(dir)) continue;
return du;
}
if(BuildConfig.DEBUG)
Log.w("CommandHelper", "Unable to find correct DiskUsage for " + dir);
return dus.get(0); // return first if none match
}
return null;
}
Expand Down
35 changes: 24 additions & 11 deletions Backbone/src/main/java/me/toolify/backbone/util/StorageHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@
import android.os.storage.StorageManager;
import android.util.Log;

import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import me.toolify.backbone.FileManagerApplication;
import me.toolify.backbone.R;
import me.toolify.backbone.model.DiskUsage;
import me.toolify.backbone.model.FileSystemObject;
import me.toolify.backbone.model.MountPoint;
import me.toolify.backbone.model.StorageVolume;

import java.io.File;
import java.lang.reflect.Method;
import java.util.HashMap;


/**
* A helper class with useful methods for deal with storages.
Expand All @@ -50,12 +53,22 @@ public final class StorageHelper {
@SuppressWarnings("boxing")
public static synchronized Object[] getStorageVolumes(Context ctx) {
if (sStorageVolumes == null) {

//IMP!! Android SDK doesn't have a "getVolumeList" but is supported by CM10.
//Use reflect to get this value (if possible)
try {
StorageManager sm = (StorageManager) ctx.getSystemService(Context.STORAGE_SERVICE);
Method method = sm.getClass().getMethod("getVolumeList"); //$NON-NLS-1$
sStorageVolumes = (Object[])method.invoke(sm);
List<Object> volumes = new ArrayList<Object>();
for(Object o : (Object[])method.invoke(sm))
{
String path = getStoragePath(o);
DiskUsage du = CommandHelper.getDiskUsage(ctx, path, null);
if(du.getTotal() <= 0) continue; // Ensure validity by checking for disk space
volumes.add(o);
}
if(volumes.size() == 0) throw new Exception("No valid volumes");
return volumes.toArray();

} catch (Exception ex) {
//Ignore. Android SDK StorageManager class doesn't have this method
Expand All @@ -68,12 +81,12 @@ public static synchronized Object[] getStorageVolumes(Context ctx) {
if(!MountPointHelper.isReadWrite(mp)) continue;
String path = mp.getMountPoint();
if(!mp.getDevice().startsWith("/")) continue;
if(path.matches("^\\/(dev|persist|cache|proc|efs|data)"))
continue;
if(path.matches("^\\/(dev|persist|cache|proc|efs|data|acct|sys)\\/"))
continue;
if(path.matches("^\\/(dev|persist|cache|proc|efs|data|acct|sys)$"))
continue;
StorageVolume sv = null;
try {
if(MountPointHelper.getMountPointDiskUsage(mp).getTotal() < 5000000)
continue;
FileSystemObject fso = CommandHelper.getFileInfo(ctx, path, null);
sv = convertToStorageVolume(fso);
} catch(Exception e3) { }
Expand Down Expand Up @@ -149,7 +162,7 @@ public static boolean isPathInStorageVolume(String path) {
/**
* Method that determines the mount path of a {@link StorageVolume} object.
*
* @param volume Object returned from {@link StorageHelper.getStorageVolumes()}
* @param volume Object returned from StorageHelper.getStorageVolumes()
* @return Mounted path
*/
public static String getStoragePath(Object volume)
Expand All @@ -167,7 +180,7 @@ public static String getStoragePath(Object volume)

/**
* Method that determines whether a path returned from
* {@link StorageHelper.getStorageVolumes()} is a valid, mounted, path.
* getStorageVolumes is a valid, mounted, path.
* @param path Path to root of mount
* @return boolean Whether path is mounted & valid
*/
Expand Down