Fix app shortcuts

This commit is contained in:
Alexander Bakker 2017-12-03 16:47:27 +01:00
parent fb24867569
commit 493ead1c68
2 changed files with 55 additions and 64 deletions

15
.idea/misc.xml generated
View file

@ -1,8 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="NullableNotNullManager"> <component name="NullableNotNullManager">
<option name="myDefaultNullable" value="android.support.annotation.Nullable" /> <option name="myDefaultNullable" value="android.support.annotation.Nullable" />
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" /> <option name="myDefaultNotNull" value="android.support.annotation.NonNull" />
@ -27,17 +24,7 @@
</value> </value>
</option> </option>
</component> </component>
<component name="ProjectLevelVcsManager" settingsEditedManually="false"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="JDK" project-jdk-type="JavaSDK">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
<OptionsSetting value="true" id="Checkout" />
<OptionsSetting value="true" id="Update" />
<OptionsSetting value="true" id="Status" />
<OptionsSetting value="true" id="Edit" />
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="JDK" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

View file

@ -19,7 +19,6 @@ import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper; import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -30,9 +29,7 @@ import java.io.FileNotFoundException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.UndeclaredThrowableException; import java.lang.reflect.UndeclaredThrowableException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Objects;
import me.impy.aegis.crypto.MasterKey; import me.impy.aegis.crypto.MasterKey;
import me.impy.aegis.db.DatabaseEntry; import me.impy.aegis.db.DatabaseEntry;
@ -97,7 +94,11 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
// init the app shortcuts and execute any pending actions
initializeAppShortcuts(); initializeAppShortcuts();
doShortcutActions();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setEnabled(true); fab.setEnabled(true);
fab.setOnClickListener(view -> { fab.setOnClickListener(view -> {
@ -175,26 +176,20 @@ public class MainActivity extends AppCompatActivity {
} }
private void onGetKeyInfoResult(int resultCode, Intent data) { private void onGetKeyInfoResult(int resultCode, Intent data) {
if (resultCode != RESULT_OK) { if (resultCode == RESULT_OK) {
return; KeyProfile keyProfile = (KeyProfile)data.getSerializableExtra("KeyProfile");
Intent intent = new Intent(this, AddProfileActivity.class);
intent.putExtra("KeyProfile", keyProfile);
startActivityForResult(intent, CODE_ADD_KEYINFO);
} }
final KeyProfile keyProfile = (KeyProfile)data.getSerializableExtra("KeyProfile");
Intent intent = new Intent(this, AddProfileActivity.class);
intent.putExtra("KeyProfile", keyProfile);
startActivityForResult(intent, CODE_ADD_KEYINFO);
} }
private void onAddKeyInfoResult(int resultCode, Intent data) { private void onAddKeyInfoResult(int resultCode, Intent data) {
if (resultCode != RESULT_OK) { if (resultCode == RESULT_OK) {
return; KeyProfile profile = (KeyProfile) data.getSerializableExtra("KeyProfile");
addKey(profile);
saveDatabase();
} }
KeyProfile profile = (KeyProfile) data.getSerializableExtra("KeyProfile");
addKey(profile);
saveDatabase();
} }
private void addKey(KeyProfile profile) { private void addKey(KeyProfile profile) {
@ -250,6 +245,24 @@ public class MainActivity extends AppCompatActivity {
} }
loadKeyProfiles(); loadKeyProfiles();
doShortcutActions();
}
private void doShortcutActions() {
Intent intent = getIntent();
String mode = intent.getStringExtra("Action");
if (mode == null || !_db.isDecrypted()) {
return;
}
switch (mode) {
case "Scan":
Intent scannerActivity = new Intent(getApplicationContext(), ScannerActivity.class);
startActivityForResult(scannerActivity, CODE_GET_KEYINFO);
break;
}
intent.removeExtra("Action");
} }
@Override @Override
@ -350,38 +363,31 @@ public class MainActivity extends AppCompatActivity {
} }
private void initializeAppShortcuts() { private void initializeAppShortcuts() {
String mode = getIntent().getStringExtra("Action"); if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
if (mode != null) { return;
Log.println(Log.DEBUG, "MODE: ", mode);
if (Objects.equals(mode, "Scan")) {
Intent scannerActivity = new Intent(getApplicationContext(), ScannerActivity.class);
startActivityForResult(scannerActivity, CODE_GET_KEYINFO);
}
} }
ShortcutManager shortcutManager = null; ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { if (shortcutManager == null) {
shortcutManager = getSystemService(ShortcutManager.class); return;
if (shortcutManager != null) { }
//TODO: Remove this line
shortcutManager.removeAllDynamicShortcuts();
if (shortcutManager.getDynamicShortcuts().size() == 0) {
// Application restored. Need to re-publish dynamic shortcuts.
Intent intent1 = new Intent(this.getBaseContext(), MainActivity.class); // TODO: Remove this line
intent1.putExtra("Action", "Scan"); shortcutManager.removeAllDynamicShortcuts();
intent1.setAction(Intent.ACTION_MAIN); 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") ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
.setShortLabel("New profile") .setShortLabel("New profile")
.setLongLabel("Add new profile") .setLongLabel("Add new profile")
.setIcon(Icon.createWithResource(this.getApplicationContext(), R.drawable.intro_scanner)) .setIcon(Icon.createWithResource(this.getApplicationContext(), R.drawable.intro_scanner))
.setIntent(intent1) .setIntent(intent)
.build(); .build();
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut)); shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut));
}
}
} }
} }
@ -393,11 +399,9 @@ public class MainActivity extends AppCompatActivity {
setTheme(R.style.AppTheme_Dark_NoActionBar); setTheme(R.style.AppTheme_Dark_NoActionBar);
restart = true; restart = true;
} }
} else { } else if (_nightMode) {
if (_nightMode) { setTheme(R.style.AppTheme_Default_NoActionBar);
setTheme(R.style.AppTheme_Default_NoActionBar); restart = true;
restart = true;
}
} }
if (restart) { if (restart) {