Move app shortcut initialization to AegisApplication

This commit is contained in:
Alexander Bakker 2017-12-26 13:04:26 +01:00
parent 89c61eab3e
commit 1ac3cf9b58
2 changed files with 33 additions and 37 deletions

View file

@ -1,8 +1,16 @@
package me.impy.aegis; package me.impy.aegis;
import android.app.Application; import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.RequiresApi;
import java.util.Collections;
import me.impy.aegis.db.DatabaseManager; import me.impy.aegis.db.DatabaseManager;
@ -13,6 +21,10 @@ public class AegisApplication extends Application {
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
initAppShortcuts();
}
} }
public DatabaseManager getDatabaseManager() { public DatabaseManager getDatabaseManager() {
@ -31,4 +43,25 @@ public class AegisApplication extends Application {
_running = true; _running = true;
return false; return false;
} }
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private void initAppShortcuts() {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
if (shortcutManager == null) {
return;
}
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("action", "scan");
intent.setAction(Intent.ACTION_MAIN);
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "shortcut_new")
.setShortLabel("New profile")
.setLongLabel("Add new profile")
.setIcon(Icon.createWithResource(this, R.drawable.intro_scanner))
.setIntent(intent)
.build();
shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut));
}
} }

View file

@ -6,11 +6,7 @@ import android.content.ClipboardManager;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.media.MediaScannerConnection; import android.media.MediaScannerConnection;
import android.os.Build;
import android.support.design.widget.BottomSheetDialog; import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
@ -25,7 +21,6 @@ import android.widget.Toast;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.UndeclaredThrowableException; import java.lang.reflect.UndeclaredThrowableException;
import java.util.Collections;
import java.util.List; import java.util.List;
import me.impy.aegis.crypto.MasterKey; import me.impy.aegis.crypto.MasterKey;
@ -67,9 +62,6 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
Toolbar toolbar = findViewById(R.id.toolbar); Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
// init the app shortcuts
initializeAppShortcuts();
// set up the key profile view // set up the key profile view
_keyProfileView = (KeyProfileView) getSupportFragmentManager().findFragmentById(R.id.key_profiles); _keyProfileView = (KeyProfileView) getSupportFragmentManager().findFragmentById(R.id.key_profiles);
_keyProfileView.setListener(this); _keyProfileView.setListener(this);
@ -513,35 +505,6 @@ public class MainActivity extends AegisActivity implements KeyProfileView.Listen
startActivityForResult(intent, CODE_DECRYPT); startActivityForResult(intent, CODE_DECRYPT);
} }
private void initializeAppShortcuts() {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
return;
}
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
if (shortcutManager == null) {
return;
}
// TODO: Remove this line
shortcutManager.removeAllDynamicShortcuts();
if (shortcutManager.getDynamicShortcuts().size() == 0) {
// Application restored. Need to re-publish dynamic shortcuts.
Intent intent = new Intent(this.getBaseContext(), MainActivity.class);
intent.putExtra("action", "scan");
intent.setAction(Intent.ACTION_MAIN);
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
.setShortLabel("New profile")
.setLongLabel("Add new profile")
.setIcon(Icon.createWithResource(this.getApplicationContext(), R.drawable.intro_scanner))
.setIntent(intent)
.build();
shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut));
}
}
private void saveDatabase() { private void saveDatabase() {
try { try {
_db.save(); _db.save();