10
10
import android .content .IntentFilter ;
11
11
import android .content .SharedPreferences ;
12
12
import android .content .pm .PackageManager ;
13
+ import android .content .pm .ShortcutInfo ;
14
+ import android .content .pm .ShortcutManager ;
13
15
import android .graphics .Bitmap ;
14
16
import android .graphics .BitmapFactory ;
17
+ import android .graphics .drawable .Icon ;
15
18
import android .net .ConnectivityManager ;
16
19
import android .net .Uri ;
17
20
import android .os .AsyncTask ;
37
40
38
41
import java .io .File ;
39
42
import java .io .IOException ;
43
+ import java .util .ArrayList ;
44
+ import java .util .Collections ;
45
+ import java .util .List ;
40
46
41
47
import ca .pkay .rcloneexplorer .BroadcastReceivers .NetworkStateReceiver ;
42
48
import ca .pkay .rcloneexplorer .Dialogs .InputDialog ;
@@ -54,6 +60,8 @@ public class MainActivity extends AppCompatActivity
54
60
private static final int READ_REQUEST_CODE = 42 ; // code when opening rclone config file
55
61
private static final int REQUEST_PERMISSION_CODE = 62 ; // code when requesting permissions
56
62
private static final int SETTINGS_CODE = 71 ; // code when coming back from settings
63
+ private final String APP_SHORTCUT_REMOTE_NAME = "arg_remote_name" ;
64
+ private final String APP_SHORTCUT_REMOTE_TYPE = "arg_remote_type" ;
57
65
private NavigationView navigationView ;
58
66
private Rclone rclone ;
59
67
private Fragment fragment ;
@@ -94,10 +102,26 @@ public void onClick(View v) {
94
102
}
95
103
});
96
104
105
+ Intent intent = getIntent ();
106
+ Bundle bundle = intent .getExtras ();
107
+
108
+ SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences (this );
109
+ int lastVersionCode = sharedPreferences .getInt (getString (R .string .pref_key_version_code ), -1 );
110
+ int currentVersionCode = BuildConfig .VERSION_CODE ;
111
+
97
112
if (!rclone .isRcloneBinaryCreated ()) {
98
113
new CreateRcloneBinary ().execute ();
114
+ } else if (lastVersionCode < currentVersionCode ) {
115
+ new CreateRcloneBinary ().execute ();
116
+ SharedPreferences .Editor editor = sharedPreferences .edit ();
117
+ editor .putInt (getString (R .string .pref_key_version_code ), currentVersionCode );
118
+ editor .apply ();
99
119
} else if (rclone .isConfigEncrypted ()) {
100
120
askForConfigPassword ();
121
+ } else if (bundle != null && bundle .containsKey (APP_SHORTCUT_REMOTE_NAME ) && bundle .containsKey (APP_SHORTCUT_REMOTE_TYPE )) {
122
+ String remoteName = bundle .getString (APP_SHORTCUT_REMOTE_NAME );
123
+ String remoteType = bundle .getString (APP_SHORTCUT_REMOTE_TYPE );
124
+ startRemote (remoteName , remoteType );
101
125
} else {
102
126
startRemotesFragment ();
103
127
}
@@ -269,14 +293,129 @@ public void requestPermissions() {
269
293
}
270
294
}
271
295
296
+ private void addRemotesToShortcutList () {
297
+ if (android .os .Build .VERSION .SDK_INT < android .os .Build .VERSION_CODES .N_MR1 ) {
298
+ return ;
299
+ }
300
+ ShortcutManager shortcutManager = getSystemService (ShortcutManager .class );
301
+ if (shortcutManager == null ) {
302
+ return ;
303
+ }
304
+ shortcutManager .removeAllDynamicShortcuts ();
305
+
306
+ List <RemoteItem > remoteItemList = rclone .getRemotes ();
307
+ List <ShortcutInfo > shortcutInfoList = new ArrayList <>();
308
+
309
+ for (RemoteItem remoteItem : remoteItemList ) {
310
+ String id = remoteItem .getName ().replaceAll (" " , "_" );
311
+
312
+ Intent intent = new Intent (Intent .ACTION_MAIN , Uri .EMPTY , this , MainActivity .class );
313
+ intent .setFlags (Intent .FLAG_ACTIVITY_CLEAR_TASK );
314
+ intent .putExtra (APP_SHORTCUT_REMOTE_NAME , remoteItem .getName ());
315
+ intent .putExtra (APP_SHORTCUT_REMOTE_TYPE , remoteItem .getType ());
316
+
317
+ ShortcutInfo shortcut = new ShortcutInfo .Builder (this , id )
318
+ .setShortLabel (remoteItem .getName ())
319
+ .setIcon (Icon .createWithResource (context , getRemoteIcon (remoteItem .getType ())))
320
+ .setIntent (intent )
321
+ .build ();
322
+ shortcutInfoList .add (shortcut );
323
+ if (shortcutInfoList .size () == 4 ) {
324
+ break ;
325
+ }
326
+ }
327
+ shortcutManager .setDynamicShortcuts (shortcutInfoList );
328
+ }
329
+
330
+ private void addRemoteToShortcutList (RemoteItem remoteItem ) {
331
+ if (android .os .Build .VERSION .SDK_INT < android .os .Build .VERSION_CODES .N_MR1 ) {
332
+ return ;
333
+ }
334
+ ShortcutManager shortcutManager = getSystemService (ShortcutManager .class );
335
+ if (shortcutManager == null ) {
336
+ return ;
337
+ }
338
+
339
+ String id = remoteItem .getName ().replaceAll (" " , "_" );
340
+
341
+ List <ShortcutInfo > shortcutInfoList = shortcutManager .getDynamicShortcuts ();
342
+ for (ShortcutInfo shortcutInfo : shortcutInfoList ) {
343
+ if (shortcutInfo .getId ().equals (id )) {
344
+ shortcutManager .reportShortcutUsed (id );
345
+ return ;
346
+ }
347
+ }
348
+
349
+ Intent intent = new Intent (Intent .ACTION_MAIN , Uri .EMPTY , this , MainActivity .class );
350
+ intent .setFlags (Intent .FLAG_ACTIVITY_CLEAR_TASK );
351
+ intent .putExtra (APP_SHORTCUT_REMOTE_NAME , remoteItem .getName ());
352
+ intent .putExtra (APP_SHORTCUT_REMOTE_TYPE , remoteItem .getType ());
353
+
354
+ ShortcutInfo shortcut = new ShortcutInfo .Builder (this , id )
355
+ .setShortLabel (remoteItem .getName ())
356
+ .setIcon (Icon .createWithResource (context , getRemoteIcon (remoteItem .getType ())))
357
+ .setIntent (intent )
358
+ .build ();
359
+
360
+ if (shortcutInfoList .size () >= 4 ) {
361
+ ShortcutInfo removeId = shortcutInfoList .get (0 );
362
+ shortcutManager .removeDynamicShortcuts (Collections .singletonList (removeId .getId ()));
363
+ }
364
+ shortcutManager .addDynamicShortcuts (Collections .singletonList (shortcut ));
365
+ shortcutManager .reportShortcutUsed (id );
366
+ }
367
+
368
+ private int getRemoteIcon (String remoteType ) {
369
+ switch (remoteType ) {
370
+ case "crypt" :
371
+ return R .drawable .ic_lock_black ;
372
+ case "amazon cloud drive" :
373
+ return R .drawable .ic_amazon ;
374
+ case "b2" :
375
+ return R .drawable .ic_b2 ;
376
+ case "drive" :
377
+ return R .drawable .ic_google_drive ;
378
+ case "dropbox" :
379
+ return R .drawable .ic_dropbox ;
380
+ case "google cloud storage" :
381
+ return R .drawable .ic_google ;
382
+ case "onedrive" :
383
+ return R .drawable .ic_onedrive ;
384
+ case "s3" :
385
+ return R .drawable .ic_amazon ;
386
+ case "yandex" :
387
+ return R .drawable .ic_yandex ;
388
+ case "box" :
389
+ return R .drawable .ic_box ;
390
+ case "sftp" :
391
+ return R .drawable .ic_terminal ;
392
+ default :
393
+ return R .drawable .ic_cloud ;
394
+ }
395
+ }
396
+
272
397
@ Override
273
398
public void onRemoteClick (RemoteItem remote ) {
399
+ startRemote (remote );
400
+ }
401
+
402
+ private void startRemote (RemoteItem remote ) {
274
403
fragment = FileExplorerFragment .newInstance (remote .getName (), remote .getType ());
275
404
FragmentTransaction transaction = getSupportFragmentManager ().beginTransaction ();
276
405
transaction .replace (R .id .flFragment , fragment );
277
406
transaction .addToBackStack (null );
278
407
transaction .commit ();
279
408
409
+ addRemoteToShortcutList (remote );
410
+ navigationView .getMenu ().getItem (0 ).setChecked (false );
411
+ }
412
+
413
+ private void startRemote (String remoteName , String remoteType ) {
414
+ fragment = FileExplorerFragment .newInstance (remoteName , remoteType );
415
+ FragmentTransaction transaction = getSupportFragmentManager ().beginTransaction ();
416
+ transaction .replace (R .id .flFragment , fragment );
417
+ transaction .commit ();
418
+
280
419
navigationView .getMenu ().getItem (0 ).setChecked (false );
281
420
}
282
421
@@ -356,6 +495,7 @@ protected void onPostExecute(Boolean success) {
356
495
if (rclone .isConfigEncrypted ()) {
357
496
askForConfigPassword ();
358
497
} else {
498
+ addRemotesToShortcutList ();
359
499
startRemotesFragment ();
360
500
}
361
501
}
@@ -390,6 +530,7 @@ protected void onPostExecute(Boolean success) {
390
530
askForConfigPassword ();
391
531
} else {
392
532
findViewById (R .id .locked_config ).setVisibility (View .GONE );
533
+ addRemotesToShortcutList ();
393
534
startRemotesFragment ();
394
535
}
395
536
}
0 commit comments