Skip to content

Commit

Permalink
Fix root display name not refreshed in SAF provider.
Browse files Browse the repository at this point in the history
This is a major rework of how remote names are handled.
  • Loading branch information
gilbsgilbs committed Jul 13, 2020
1 parent 42f9f0c commit a9aee9d
Show file tree
Hide file tree
Showing 18 changed files with 106 additions and 105 deletions.
10 changes: 6 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ android {
}

project.ext.versionCodes = [
'armeabi-v7a': 6,
'arm64-v8a': 7,
'x86': 8,
'x86_64': 9]
'armeabi-v7a': 6,
'arm64-v8a': 7,
'x86': 8,
'x86_64': 9
]

android.applicationVariants.all { variant ->
variant.outputs.each { output ->
Expand Down Expand Up @@ -111,6 +112,7 @@ dependencies {
implementation 'org.markdownj:markdownj-core:0.4'
implementation 'jp.wasabeef:recyclerview-animators:2.3.0'
implementation 'com.github.GrenderG:Toasty:1.3.0'
implementation 'commons-io:commons-io:2.7'
// Firebase & Crashlytics
rcxImplementation 'com.google.firebase:firebase-analytics:17.3.0'
rcxImplementation 'com.google.firebase:firebase-crashlytics:17.0.0-beta03'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public static void populateAppShortcuts(Context context, List<RemoteItem> remote
Set<String> shortcutSet = new HashSet<>();
List<ShortcutInfo> shortcutInfoList = new ArrayList<>();

RemoteItem.prepareDisplay(context, remotes);
for (RemoteItem remoteItem : remotes) {
String id = getUniqueIdFromString(remoteItem.getName());

Expand All @@ -51,7 +50,7 @@ public static void populateAppShortcuts(Context context, List<RemoteItem> remote
intent.putExtra(APP_SHORTCUT_REMOTE_NAME, remoteItem.getName());

ShortcutInfo shortcut = new ShortcutInfo.Builder(context, id)
.setShortLabel(remoteItem.getDisplayName())
.setShortLabel(remoteItem.getName())
.setIcon(Icon.createWithResource(context, AppShortcutsHelper.getRemoteIcon(remoteItem.getType(), remoteItem.isCrypt())))
.setIntent(intent)
.build();
Expand Down Expand Up @@ -164,7 +163,7 @@ public static void addRemoteToAppShortcuts(Context context, RemoteItem remoteIte
intent.putExtra(APP_SHORTCUT_REMOTE_NAME, remoteItem.getName());

ShortcutInfo shortcut = new ShortcutInfo.Builder(context, id)
.setShortLabel(remoteItem.getDisplayName())
.setShortLabel(remoteItem.getName())
.setIcon(Icon.createWithResource(context, AppShortcutsHelper.getRemoteIcon(remoteItem.getType(), remoteItem.isCrypt())))
.setIntent(intent)
.build();
Expand All @@ -184,7 +183,7 @@ public static void addRemoteToHomeScreen(Context context, RemoteItem remoteItem)
intent.putExtra(APP_SHORTCUT_REMOTE_NAME, remoteItem.getName());

ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, id)
.setShortLabel(remoteItem.getDisplayName())
.setShortLabel(remoteItem.getName())
.setIcon(IconCompat.createWithResource(context, AppShortcutsHelper.getRemoteIcon(remoteItem.getType(), remoteItem.isCrypt())))
.setIntent(intent)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = ((FragmentActivity) context).getLayoutInflater();
view = inflater.inflate(R.layout.dialog_remote_properties, null);

((TextView) view.findViewById(R.id.remote_name)).setText(remote.getDisplayName());
((TextView) view.findViewById(R.id.remote_name)).setText(remote.getName());

View storageContainer = view.findViewById(R.id.remote_storage_container);
remoteStorageStats = view.findViewById(R.id.remote_storage_stats);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
breadcrumbView = ((FragmentActivity) context).findViewById(R.id.breadcrumb_view);
breadcrumbView.setOnClickListener(this);
breadcrumbView.setVisibility(View.VISIBLE);
breadcrumbView.addCrumb(remote.getDisplayName(), "//" + remoteName);
breadcrumbView.addCrumb(remote.getName(), "//" + remoteName);
if (savedInstanceState != null) {
if (!directoryObject.getCurrentPath().equals("//" + remoteName)) {
breadcrumbView.buildBreadCrumbsFromPath(directoryObject.getCurrentPath());
Expand Down Expand Up @@ -935,7 +935,7 @@ private void cancelMoveClicked() {
if (!moveStartPath.equals("//" + remoteName)) {
breadcrumbView.buildBreadCrumbsFromPath(directoryObject.getCurrentPath());
}
breadcrumbView.addCrumb(remote.getDisplayName(), "//" + remoteName);
breadcrumbView.addCrumb(remote.getName(), "//" + remoteName);
moveStartPath = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ private void refreshRemotes() {
private List<RemoteItem> filterRemotes() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Set<String> hiddenRemotes = sharedPreferences.getStringSet(getString(R.string.shared_preferences_hidden_remotes), new HashSet<>());
Set<String> renamedRemotes = sharedPreferences.getStringSet(getString(R.string.pref_key_renamed_remotes), new HashSet<>());
remotes = rclone.getRemotes();
if (hiddenRemotes != null && !hiddenRemotes.isEmpty()) {
ArrayList<RemoteItem> toBeHidden = new ArrayList<>();
Expand All @@ -314,13 +313,6 @@ private List<RemoteItem> filterRemotes() {
remotes.removeAll(toBeHidden);
}
Collections.sort(remotes);
for(RemoteItem item : remotes) {
if(renamedRemotes.contains(item.getName())) {
String displayName = sharedPreferences.getString(
getString(R.string.pref_key_renamed_remote_prefix, item.getName()), item.getName());
item.setDisplayName(displayName);
}
}
return remotes;
}

Expand Down Expand Up @@ -469,21 +461,14 @@ private void renameRemote(final RemoteItem remoteItem) {
builder = new AlertDialog.Builder(context);
}

final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
final EditText remoteNameEdit = new EditText(context);
String initialText = remoteItem.getDisplayName();
String initialText = remoteItem.getName();
remoteNameEdit.setText(initialText);
builder.setView(remoteNameEdit);
builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.select, (dialog, which) -> {
String displayName = remoteNameEdit.getText().toString();
Set<String> renamedRemotes = pref.getStringSet(getString(R.string.pref_key_renamed_remotes), new HashSet<>());
renamedRemotes.add(remoteItem.getName());
pref.edit()
.putString(getString(R.string.pref_key_renamed_remote_prefix, remoteItem.getName()), displayName)
.putStringSet(getString(R.string.pref_key_renamed_remotes), renamedRemotes)
.apply();
remoteItem.setDisplayName(displayName);
String newName = remoteNameEdit.getText().toString();
rclone.renameRemote(remoteItem.getName(), newName);
refreshRemotes();
});
builder.setTitle(R.string.rename_remote);
Expand All @@ -498,7 +483,7 @@ private void deleteRemote(final RemoteItem remoteItem) {
builder = new AlertDialog.Builder(context);
}
builder.setTitle(R.string.delete_remote_title);
builder.setMessage(remoteItem.getDisplayName());
builder.setMessage(remoteItem.getName());
builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.delete, (dialog, which) -> new DeleteRemote(context, remoteItem).execute());
builder.show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
((FragmentActivity) context).setTitle(getString(R.string.remotes_toolbar_title));
Rclone rclone = new Rclone(getContext());
remotes = rclone.getRemotes();
RemoteItem.prepareDisplay(getContext(), remotes);
Collections.sort(remotes);
}

Expand Down
26 changes: 4 additions & 22 deletions app/src/main/java/ca/pkay/rcloneexplorer/Items/RemoteItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,27 +249,6 @@ public boolean isRemoteType(int ...remotes) {
return isSameType;
}

public String getDisplayName() {
return displayName != null ? displayName : name;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public static List<RemoteItem> prepareDisplay(Context context, List<RemoteItem> items) {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
Set<String> renamedRemotes = pref.getStringSet(context.getString(R.string.pref_key_renamed_remotes), new HashSet<>());
for(RemoteItem item : items) {
if(renamedRemotes.contains(item.name)) {
String displayName = pref.getString(
context.getString(R.string.pref_key_renamed_remote_prefix, item.name), item.name);
item.displayName = displayName;
}
}
return items;
}

private int getTypeFromString(String type) {
switch (type) {
case SafConstants.SAF_REMOTE_NAME:
Expand Down Expand Up @@ -407,7 +386,10 @@ public int compareTo(@NonNull RemoteItem remoteItem) {
} else if (!this.isPinned && remoteItem.isPinned) {
return 1;
}
return getDisplayName().toLowerCase().compareTo(remoteItem.getDisplayName().toLowerCase());

String self = getName().toLowerCase();
String other = remoteItem.getName().toLowerCase();
return self.compareTo(other);
}

@Override
Expand Down
37 changes: 8 additions & 29 deletions app/src/main/java/ca/pkay/rcloneexplorer/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -366,18 +366,9 @@ private void pinRemotesToDrawer() {

List<RemoteItem> remoteItems = rclone.getRemotes();
Collections.sort(remoteItems);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Set<String> renamedRemotes = sharedPreferences.getStringSet(getString(R.string.pref_key_renamed_remotes), new HashSet<>());
for(RemoteItem item : remoteItems) {
if(renamedRemotes.contains(item.getName())) {
String displayName = sharedPreferences.getString(
getString(R.string.pref_key_renamed_remote_prefix, item.getName()), item.getName());
item.setDisplayName(displayName);
}
}
for (RemoteItem remoteItem : remoteItems) {
if (remoteItem.isDrawerPinned()) {
MenuItem menuItem = subMenu.add(R.id.nav_pinned, availableDrawerPinnedRemoteId, Menu.NONE, remoteItem.getDisplayName());
MenuItem menuItem = subMenu.add(R.id.nav_pinned, availableDrawerPinnedRemoteId, Menu.NONE, remoteItem.getName());
drawerPinnedRemoteIds.put(availableDrawerPinnedRemoteId, remoteItem);
availableDrawerPinnedRemoteId++;
menuItem.setIcon(remoteItem.getRemoteIcon());
Expand Down Expand Up @@ -735,9 +726,6 @@ protected void onPostExecute(Boolean success) {
}

private class RefreshLocalAliases extends AsyncTask<Void, Void, Boolean> {

private String EMULATED = "5d44cd8d-397c-4107-b79b-17f2b6a071e8";

private LoadingDialog loadingDialog;

protected boolean isRequired() {
Expand Down Expand Up @@ -790,14 +778,10 @@ protected Boolean doInBackground(Void... aVoid) {
}
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
Set<String> generated = pref.getStringSet(getString(R.string.pref_key_local_alias_remotes), new HashSet<>());
Set<String> renamed = pref.getStringSet(getString(R.string.pref_key_renamed_remotes), new HashSet<>());
SharedPreferences.Editor editor = pref.edit();
for(String remote : generated) {
rclone.deleteRemote(remote);
renamed.remove(remote);
editor.remove(getString(R.string.pref_key_renamed_remote_prefix, remote));
}
editor.putStringSet(getString(R.string.pref_key_renamed_remotes), renamed);
editor.apply();
File[] dirs = context.getExternalFilesDirs(null);
for(File file : dirs) {
Expand Down Expand Up @@ -833,23 +817,22 @@ private File getVolumeRoot(File file) {
private void addLocalRemote(File root) throws IOException {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
String name = root.getCanonicalPath();
String id = Environment.isExternalStorageEmulated(root) ? EMULATED : UUID.randomUUID().toString();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
StorageVolume storageVolume = storageManager.getStorageVolume(root);
name = storageVolume.getDescription(context);
if (null != storageVolume.getUuid()) {
id = storageVolume.getUuid();
String description = storageVolume != null ? storageVolume.getDescription(context) : null;
if (description != null) {
name = rclone.getUniqueRemoteName(description);
}
}

String path = root.getAbsolutePath();
ArrayList<String> options = new ArrayList<>();
options.add(id);
options.add(name);
options.add("alias");
options.add("remote");
options.add(path);
FLog.d(TAG, "Adding local remote [%s] remote = %s", id, path);
FLog.d(TAG, "Adding local remote [%s] remote = %s", name, path);
Process process = rclone.configCreate(options);
try {
process.waitFor();
Expand All @@ -861,15 +844,11 @@ private void addLocalRemote(File root) throws IOException {
FLog.e(TAG, "addLocalRemote: process error", e);
return;
}
Set<String> renamedRemotes = pref.getStringSet(getString(R.string.pref_key_renamed_remotes), new HashSet<>());
Set<String> pinnedRemotes = pref.getStringSet(getString(R.string.shared_preferences_drawer_pinned_remotes), new HashSet<>());
Set<String> generatedRemotes = pref.getStringSet(getString(R.string.pref_key_local_alias_remotes), new HashSet<>());
renamedRemotes.add(id);
pinnedRemotes.add(id);
generatedRemotes.add(id);
pinnedRemotes.add(name);
generatedRemotes.add(name);
pref.edit()
.putStringSet(getString(R.string.pref_key_renamed_remotes), renamedRemotes)
.putString(getString(R.string.pref_key_renamed_remote_prefix, id), name)
.putStringSet(getString(R.string.shared_preferences_drawer_pinned_remotes), pinnedRemotes)
.putStringSet(getString(R.string.pref_key_local_alias_remotes), generatedRemotes)
.apply();
Expand Down
64 changes: 63 additions & 1 deletion app/src/main/java/ca/pkay/rcloneexplorer/Rclone.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import io.github.x0b.safdav.SafAccessProvider;
import io.github.x0b.safdav.SafDAVServer;
import io.github.x0b.safdav.file.SafConstants;

import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -407,7 +409,6 @@ public Process configCreate(List<String> options) {

System.arraycopy(opt, 0, commandWithOptions, command.length, opt.length);


try {
return Runtime.getRuntime().exec(commandWithOptions);
} catch (IOException e) {
Expand Down Expand Up @@ -1148,6 +1149,67 @@ public void exportConfigFile(Uri uri) throws IOException {
outputStream.close();
}

public boolean renameRemote(String remoteName, String newName) {
// RClone uses this library to parse its config file: https://github.com/unknwon/goconfig
// It's very similar to the INI syntax, where section corresponds to a
// remote. Sections can contain any unicode character except for line
// breaks and are completely unescaped.

boolean found = false;
String searchedLine = "[" + remoteName + "]";
File file = new File(rcloneConf);
List<String> configContent;
List<String> newConfigContent;

try {
configContent = FileUtils.readLines(file, "UTF-8");
} catch (IOException e) {
Log.e(TAG, "Couldn't parse RClone config.", e);
return false;
}

newConfigContent = new ArrayList<>(configContent.size());
for (String line : configContent) {
if (line.trim().equals(searchedLine)) {
newConfigContent.add("[" + newName + "]");
found = true;
}
else {
newConfigContent.add(line);
}
}

if (!found) {
return false;
}

try {
FileUtils.writeLines(file, newConfigContent);
} catch (IOException e) {
Log.e(TAG, "Couldn't write RClone config.", e);
return false;
}

return true;
}

public String getUniqueRemoteName(String desiredName) {
List<RemoteItem> remotes = getRemotes();
Set<String> remoteNames = new HashSet<>(remotes.size());
for (RemoteItem remoteItem : remotes) {
remoteNames.add(remoteItem.getName());
}
if (!remoteNames.contains(desiredName)) {
return desiredName;
}
for (int i = 1;;++i) {
String remoteName = desiredName + " " + i;
if (!remoteNames.contains(remoteName)) {
return remoteName;
}
}
}

/**
* Prefixes local remotes with a base path on the primary external storage.
* @param item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void onBindViewHolder(@NonNull final ViewHolder holder, final int positio
RemoteItem item = remotes.get(position);
boolean isPinned = item.isPinned();
holder.remoteName = item.getName();
holder.tvName.setText(item.getDisplayName());
holder.tvName.setText(item.getName());

int icon = item.getRemoteIcon();
holder.ivIcon.setImageDrawable(view.getResources().getDrawable(icon));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void onBindViewHolder(@NonNull final ViewHolder holder, final int positio
String remoteName = remotes.get(position).getName();
boolean isPinned = remotes.get(position).isPinned();
holder.remoteName = remoteName;
holder.tvName.setText(remotes.get(position).getDisplayName());
holder.tvName.setText(remotes.get(position).getName());

int icon = remotes.get(position).getRemoteIcon();
holder.ivIcon.setImageDrawable(view.getResources().getDrawable(icon));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,11 @@ private void setRemote() {
return;
}

RemoteItem.prepareDisplay(context, remotes);
Collections.sort(remotes, (a, b) -> a.getDisplayName().compareTo(b.getDisplayName()));
Collections.sort(remotes, (a, b) -> a.getName().compareTo(b.getName()));
String[] options = new String[remotes.size()];
int i = 0;
for (RemoteItem remote : remotes) {
options[i++] = remote.getDisplayName();
options[i++] = remote.getName();
}

AlertDialog.Builder builder;
Expand Down
Loading

0 comments on commit a9aee9d

Please sign in to comment.