Refactor Timeout, delete useless Intents class

This commit is contained in:
J-Jamet
2018-03-17 20:18:06 +01:00
parent 8c38b361ea
commit 9ee9063c4d
6 changed files with 34 additions and 63 deletions

View File

@@ -51,7 +51,6 @@ import com.keepassdroid.database.Database;
import com.keepassdroid.database.PwDatabase; import com.keepassdroid.database.PwDatabase;
import com.keepassdroid.database.PwEntry; import com.keepassdroid.database.PwEntry;
import com.keepassdroid.database.exception.SamsungClipboardException; import com.keepassdroid.database.exception.SamsungClipboardException;
import com.keepassdroid.intents.Intents;
import com.keepassdroid.password.PasswordActivity; import com.keepassdroid.password.PasswordActivity;
import com.keepassdroid.services.NotificationCopyingService; import com.keepassdroid.services.NotificationCopyingService;
import com.keepassdroid.settings.PreferencesUtil; import com.keepassdroid.settings.PreferencesUtil;
@@ -74,6 +73,9 @@ import static com.keepassdroid.settings.PreferencesUtil.isClipboardNotifications
public class EntryActivity extends LockingHideActivity { public class EntryActivity extends LockingHideActivity {
public static final String KEY_ENTRY = "entry"; public static final String KEY_ENTRY = "entry";
public static final String COPY_USERNAME = "com.keepassdroid.copy_username";
public static final String COPY_PASSWORD = "com.keepassdroid.copy_password";
private ImageView titleIconView; private ImageView titleIconView;
private TextView titleView; private TextView titleView;
private EntryContentsView entryContentsView; private EntryContentsView entryContentsView;
@@ -167,17 +169,16 @@ public class EntryActivity extends LockingHideActivity {
} }
mIntentReceiver = new BroadcastReceiver() { mIntentReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String action = intent.getAction(); String action = intent.getAction();
if ( action != null) { if ( action != null) {
if (action.equals(Intents.COPY_USERNAME)) { if (action.equals(COPY_USERNAME)) {
String username = mEntry.getUsername(); String username = mEntry.getUsername();
if (username.length() > 0) { if (username.length() > 0) {
timeoutCopyToClipboard(username); timeoutCopyToClipboard(username);
} }
} else if (action.equals(Intents.COPY_PASSWORD)) { } else if (action.equals(COPY_PASSWORD)) {
String password = mEntry.getPassword(); String password = mEntry.getPassword();
if (password.length() > 0) { if (password.length() > 0) {
timeoutCopyToClipboard(password); timeoutCopyToClipboard(password);
@@ -188,8 +189,8 @@ public class EntryActivity extends LockingHideActivity {
}; };
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();
filter.addAction(Intents.COPY_USERNAME); filter.addAction(COPY_USERNAME);
filter.addAction(Intents.COPY_PASSWORD); filter.addAction(COPY_PASSWORD);
registerReceiver(mIntentReceiver, filter); registerReceiver(mIntentReceiver, filter);
} }

View File

@@ -1,28 +0,0 @@
/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/package com.keepassdroid.intents;
public class Intents {
public static final String TIMEOUT = "com.keepassdroid.timeout";
public static final String COPY_USERNAME = "com.keepassdroid.copy_username";
public static final String COPY_PASSWORD = "com.keepassdroid.copy_password";
public static final String OPEN_INTENTS_FILE_BROWSE = "org.openintents.action.PICK_FILE";
}

View File

@@ -30,7 +30,7 @@ import android.os.IBinder;
import android.util.Log; import android.util.Log;
import com.keepassdroid.app.App; import com.keepassdroid.app.App;
import com.keepassdroid.intents.Intents; import com.keepassdroid.timeout.Timeout;
public class TimeoutService extends Service { public class TimeoutService extends Service {
private static final String TAG = "KeePassDroid Timer"; private static final String TAG = "KeePassDroid Timer";
@@ -44,42 +44,36 @@ public class TimeoutService extends Service {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String action = intent.getAction(); String action = intent.getAction();
if ( action != null && action.equals(Timeout.TIMEOUT) ) {
if ( action.equals(Intents.TIMEOUT) ) { timeout();
timeout(context);
} }
} }
}; };
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();
filter.addAction(Intents.TIMEOUT); filter.addAction(Timeout.TIMEOUT);
registerReceiver(mIntentReceiver, filter); registerReceiver(mIntentReceiver, filter);
} }
@Override @Override
public void onStart(Intent intent, int startId) { public void onStart(Intent intent, int startId) {
super.onStart(intent, startId); super.onStart(intent, startId);
Log.d(TAG, "Timeout service started"); Log.d(TAG, "Timeout service started");
} }
private void timeout(Context context) { private void timeout() {
Log.d(TAG, "Timeout"); Log.d(TAG, "Timeout");
App.setShutdown(); App.setShutdown();
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancelAll(); if (nm != null)
nm.cancelAll();
stopSelf(); stopSelf();
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
Log.d(TAG, "Timeout service stopped"); Log.d(TAG, "Timeout service stopped");
unregisterReceiver(mIntentReceiver); unregisterReceiver(mIntentReceiver);
} }

View File

@@ -1,4 +1,4 @@
package com.keepassdroid.timers; package com.keepassdroid.timeout;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.PendingIntent; import android.app.PendingIntent;
@@ -8,20 +8,20 @@ import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import com.keepassdroid.intents.Intents;
import com.keepassdroid.services.TimeoutService; import com.keepassdroid.services.TimeoutService;
import com.kunzisoft.keepass.R; import com.kunzisoft.keepass.R;
public class Timeout { public class Timeout {
public static final String TIMEOUT = "com.keepassdroid.timeout";
private static final int REQUEST_ID = 0; private static final int REQUEST_ID = 0;
private static final long DEFAULT_TIMEOUT = 5 * 60 * 1000; // 5 minutes private static final long DEFAULT_TIMEOUT = 5 * 60 * 1000; // 5 minutes
private static String TAG = "KeePass Timeout"; private static String TAG = "KeePass Timeout";
private static PendingIntent buildIntent(Context ctx) { private static PendingIntent buildIntent(Context ctx) {
Intent intent = new Intent(Intents.TIMEOUT); Intent intent = new Intent(TIMEOUT);
PendingIntent sender = PendingIntent.getBroadcast(ctx, REQUEST_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT); return PendingIntent.getBroadcast(ctx, REQUEST_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
return sender;
} }
public static void start(Context ctx) { public static void start(Context ctx) {
@@ -46,16 +46,20 @@ public class Timeout {
AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE); AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
Log.d(TAG, "Timeout start"); Log.d(TAG, "Timeout start");
am.set(AlarmManager.RTC, triggerTime, buildIntent(ctx)); if (am != null) {
} am.set(AlarmManager.RTC, triggerTime, buildIntent(ctx));
}
}
public static void cancel(Context ctx) { public static void cancel(Context ctx) {
AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE); AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
Log.d(TAG, "Timeout cancel"); Log.d(TAG, "Timeout cancel");
am.cancel(buildIntent(ctx)); if (am != null) {
am.cancel(buildIntent(ctx));
}
ctx.stopService(new Intent(ctx, TimeoutService.class)); ctx.stopService(new Intent(ctx, TimeoutService.class));
} }
} }

View File

@@ -28,7 +28,6 @@ import com.keepassdroid.password.PasswordActivity;
import com.kunzisoft.keepass.R; import com.kunzisoft.keepass.R;
import com.keepassdroid.app.App; import com.keepassdroid.app.App;
import com.keepassdroid.compat.EditorCompat; import com.keepassdroid.compat.EditorCompat;
import com.keepassdroid.timers.Timeout;
public class TimeoutHelper { public class TimeoutHelper {

View File

@@ -32,7 +32,6 @@ import android.view.View;
import com.keepassdroid.compat.ContentResolverCompat; import com.keepassdroid.compat.ContentResolverCompat;
import com.keepassdroid.compat.StorageAF; import com.keepassdroid.compat.StorageAF;
import com.keepassdroid.fileselect.BrowserDialog; import com.keepassdroid.fileselect.BrowserDialog;
import com.keepassdroid.intents.Intents;
import com.keepassdroid.utils.Interaction; import com.keepassdroid.utils.Interaction;
import com.keepassdroid.utils.UriUtil; import com.keepassdroid.utils.UriUtil;
@@ -44,6 +43,8 @@ public class KeyFileHelper {
private static final String TAG = "KeyFileHelper"; private static final String TAG = "KeyFileHelper";
public static final String OPEN_INTENTS_FILE_BROWSE = "org.openintents.action.PICK_FILE";
private static final int GET_CONTENT = 25745; private static final int GET_CONTENT = 25745;
private static final int OPEN_DOC = 25845; private static final int OPEN_DOC = 25845;
private static final int FILE_BROWSE = 25645; private static final int FILE_BROWSE = 25645;
@@ -129,8 +130,8 @@ public class KeyFileHelper {
private boolean lookForOpenIntentsFilePicker(@Nullable Uri dataUri) { private boolean lookForOpenIntentsFilePicker(@Nullable Uri dataUri) {
boolean showBrowser = false; boolean showBrowser = false;
try { try {
if (Interaction.isIntentAvailable(activity, Intents.OPEN_INTENTS_FILE_BROWSE)) { if (Interaction.isIntentAvailable(activity, OPEN_INTENTS_FILE_BROWSE)) {
Intent i = new Intent(Intents.OPEN_INTENTS_FILE_BROWSE); Intent i = new Intent(OPEN_INTENTS_FILE_BROWSE);
// Get file path parent if possible // Get file path parent if possible
if (dataUri != null if (dataUri != null
&& dataUri.toString().length() > 0 && dataUri.toString().length() > 0