Skip to content

Commit

Permalink
Merge pull request #175 from kaczmarkiewiczp/customizable-app-shortcuts
Browse files Browse the repository at this point in the history
Customizable app shortcuts
  • Loading branch information
patrykcoding authored Jul 15, 2018
2 parents e1649a5 + a129153 commit c88f582
Show file tree
Hide file tree
Showing 10 changed files with 505 additions and 145 deletions.
7 changes: 4 additions & 3 deletions .idea/assetWizardSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "ca.pkay.rcloneexplorer"
minSdkVersion 21
targetSdkVersion 27
versionCode 23
versionName "1.5.0-DEV"
versionCode 24
versionName "1.6.0-DEV"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
235 changes: 235 additions & 0 deletions app/src/main/java/ca/pkay/rcloneexplorer/AppShortcutsHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
package ca.pkay.rcloneexplorer;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.annotation.RequiresApi;
import android.support.v4.content.pm.ShortcutInfoCompat;
import android.support.v4.content.pm.ShortcutManagerCompat;
import android.support.v4.graphics.drawable.IconCompat;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import ca.pkay.rcloneexplorer.Items.RemoteItem;

public class AppShortcutsHelper {

public static final String APP_SHORTCUT_REMOTE_NAME = "arg_remote_name";

public static void populateAppShortcuts(Context context, List<RemoteItem> remotes) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N_MR1) {
return;
}

ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (shortcutManager == null) {
return;
}

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();

Set<String> shortcutSet = new HashSet<>();
List<ShortcutInfo> shortcutInfoList = new ArrayList<>();

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

Intent intent = new Intent(Intent.ACTION_MAIN, Uri.EMPTY, context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(APP_SHORTCUT_REMOTE_NAME, remoteItem.getName());

ShortcutInfo shortcut = new ShortcutInfo.Builder(context, id)
.setShortLabel(remoteItem.getName())
.setIcon(Icon.createWithResource(context, AppShortcutsHelper.getRemoteIcon(remoteItem.getType(), remoteItem.isCrypt())))
.setIntent(intent)
.build();

shortcutInfoList.add(shortcut);
shortcutSet.add(id);

if (shortcutInfoList.size() >= 4) {
break;
}
}

shortcutManager.setDynamicShortcuts(shortcutInfoList);
editor.putStringSet(context.getString(R.string.shared_preferences_app_shortcuts), shortcutSet);
editor.apply();
}

public static void removeAllAppShortcuts(Context context) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N_MR1) {
return;
}

ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (shortcutManager == null) {
return;
}

shortcutManager.removeAllDynamicShortcuts();

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(context.getString(R.string.shared_preferences_app_shortcuts));
editor.apply();
}

public static void removeAppShortcut(Context context, String remoteName) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N_MR1) {
return;
}

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Set<String> appShortcutIds = sharedPreferences.getStringSet(context.getString(R.string.shared_preferences_app_shortcuts), new HashSet<String>());
String id = getUniqueIdFromString(remoteName);

if (!appShortcutIds.contains(id)) {
return;
}

ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (shortcutManager == null) {
return;
}

List<ShortcutInfo> shortcutInfoList = shortcutManager.getDynamicShortcuts();
for (ShortcutInfo shortcutInfo : shortcutInfoList) {
if (shortcutInfo.getId().equals(id)) {
shortcutManager.removeDynamicShortcuts(Collections.singletonList(shortcutInfo.getId()));
return;
}
}

SharedPreferences.Editor editor = sharedPreferences.edit();
appShortcutIds.remove(id);
Set<String> updateAppShortcutIds = new HashSet<>(appShortcutIds);
editor.putStringSet(context.getString(R.string.shared_preferences_app_shortcuts), updateAppShortcutIds);
editor.apply();
}

public static void removeAppShortcutIds(Context context, List<String> ids) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N_MR1) {
return;
}

ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (shortcutManager == null) {
return;
}

shortcutManager.removeDynamicShortcuts(ids);
}

public static void reportAppShortcutUsage(Context context, String remoteName) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N_MR1) {
return;
}

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Set<String> appShortcutIds = sharedPreferences.getStringSet(context.getString(R.string.shared_preferences_app_shortcuts), new HashSet<String>());

String id = getUniqueIdFromString(remoteName);

if (appShortcutIds.contains(id)) {
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (shortcutManager == null) {
return;
}
shortcutManager.reportShortcutUsed(id);
}
}

@RequiresApi(api = Build.VERSION_CODES.N_MR1)
public static void addRemoteToAppShortcuts(Context context, RemoteItem remoteItem, String id) {
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (shortcutManager == null) {
return;
}

Intent intent = new Intent(Intent.ACTION_MAIN, Uri.EMPTY, context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(APP_SHORTCUT_REMOTE_NAME, remoteItem.getName());

ShortcutInfo shortcut = new ShortcutInfo.Builder(context, id)
.setShortLabel(remoteItem.getName())
.setIcon(Icon.createWithResource(context, AppShortcutsHelper.getRemoteIcon(remoteItem.getType(), remoteItem.isCrypt())))
.setIntent(intent)
.build();

shortcutManager.addDynamicShortcuts(Collections.singletonList(shortcut));
}

public static boolean isRequestPinShortcutSupported(Context context) {
return ShortcutManagerCompat.isRequestPinShortcutSupported(context);
}

public static void addRemoteToHomeScreen(Context context, RemoteItem remoteItem) {
String id = getUniqueIdFromString(remoteItem.getName());

Intent intent = new Intent(Intent.ACTION_MAIN, Uri.EMPTY, context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(APP_SHORTCUT_REMOTE_NAME, remoteItem.getName());

ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, id)
.setShortLabel(remoteItem.getName())
.setIcon(IconCompat.createWithResource(context, AppShortcutsHelper.getRemoteIcon(remoteItem.getType(), remoteItem.isCrypt())))
.setIntent(intent)
.build();

ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
}

public static String getUniqueIdFromString(String s) {
StringBuilder hash = new StringBuilder();
char[] chars = s.toCharArray();

for (char c : chars) {
int ascii = (int) c;
hash.append(ascii);
hash.append(".");
}

return hash.toString();
}

private static int getRemoteIcon(int remoteType, boolean isCrypt) {
if (isCrypt) {
return R.mipmap.ic_shortcut_lock;
}
switch (remoteType) {
case RemoteItem.AMAZON_DRIVE:
return R.mipmap.ic_shortcut_amazon;
case RemoteItem.GOOGLE_DRIVE:
return R.mipmap.ic_shortcut_drive;
case RemoteItem.DROPBOX:
return R.mipmap.ic_shortcut_dropbox;
case RemoteItem.GOOGLE_CLOUD_STORAGE:
return R.mipmap.ic_shortcut_google;
case RemoteItem.ONEDRIVE:
return R.mipmap.ic_shortcut_onedrive;
case RemoteItem.S3:
return R.mipmap.ic_shortcut_amazon;
case RemoteItem.BOX:
return R.mipmap.ic_shortcut_box;
case RemoteItem.SFTP:
return R.mipmap.ic_shortcut_terminal;
case RemoteItem.LOCAL:
return R.mipmap.ic_shortcut_local;
default:
return R.mipmap.ic_shortcut_cloud;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;
import java.util.Set;

import ca.pkay.rcloneexplorer.AppShortcutsHelper;
import ca.pkay.rcloneexplorer.Items.RemoteItem;
import ca.pkay.rcloneexplorer.MainActivity;
import ca.pkay.rcloneexplorer.R;
Expand Down Expand Up @@ -131,12 +132,18 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
remotes = rclone.getRemotes();
Collections.sort(remotes);
recyclerViewAdapter.newData(remotes);
if (remotes.size() == 1) {
AppShortcutsHelper.populateAppShortcuts(context, remotes);
}
break;
case CONFIG_RECREATE_REQ_CODE:
remotes = rclone.getRemotes();
Collections.sort(remotes);
recyclerViewAdapter = new RemotesRecyclerViewAdapter(remotes, clickListener, this);
refreshFragment();
if (remotes.size() == 1) {
AppShortcutsHelper.populateAppShortcuts(context, remotes);
}
break;
}
}
Expand Down Expand Up @@ -192,8 +199,10 @@ public boolean onMenuItemClick(MenuItem item) {
pinRemote(remoteItem);
}
break;
case R.id.action_add_to_home_screen:
AppShortcutsHelper.addRemoteToHomeScreen(context, remoteItem);
break;
default:
pinRemote(remoteItem);
return false;
}
return true;
Expand All @@ -207,6 +216,10 @@ public boolean onMenuItemClick(MenuItem item) {
} else {
pinAction.setTitle(R.string.pin_to_the_top);
}
if (!AppShortcutsHelper.isRequestPinShortcutSupported(context)) {
MenuItem addToHomeScreenAction = popupMenu.getMenu().findItem(R.id.action_add_to_home_screen);
addToHomeScreenAction.setVisible(false);
}
}

private void pinRemote(RemoteItem remoteItem) {
Expand Down Expand Up @@ -298,9 +311,7 @@ protected void onPostExecute(Void aVoid) {
editor.apply();
}

if (context instanceof MainActivity) {
((MainActivity)context).removeRemoteFromShortcutList(remoteItem);
}
AppShortcutsHelper.removeAppShortcut(context, remoteItem.getName());

recyclerViewAdapter.removeItem(remoteItem);

Expand Down
Loading

0 comments on commit c88f582

Please sign in to comment.