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.PwEntry;
import com.keepassdroid.database.exception.SamsungClipboardException;
import com.keepassdroid.intents.Intents;
import com.keepassdroid.password.PasswordActivity;
import com.keepassdroid.services.NotificationCopyingService;
import com.keepassdroid.settings.PreferencesUtil;
@@ -74,6 +73,9 @@ import static com.keepassdroid.settings.PreferencesUtil.isClipboardNotifications
public class EntryActivity extends LockingHideActivity {
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 TextView titleView;
private EntryContentsView entryContentsView;
@@ -167,17 +169,16 @@ public class EntryActivity extends LockingHideActivity {
}
mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ( action != null) {
if (action.equals(Intents.COPY_USERNAME)) {
if (action.equals(COPY_USERNAME)) {
String username = mEntry.getUsername();
if (username.length() > 0) {
timeoutCopyToClipboard(username);
}
} else if (action.equals(Intents.COPY_PASSWORD)) {
} else if (action.equals(COPY_PASSWORD)) {
String password = mEntry.getPassword();
if (password.length() > 0) {
timeoutCopyToClipboard(password);
@@ -188,8 +189,8 @@ public class EntryActivity extends LockingHideActivity {
};
IntentFilter filter = new IntentFilter();
filter.addAction(Intents.COPY_USERNAME);
filter.addAction(Intents.COPY_PASSWORD);
filter.addAction(COPY_USERNAME);
filter.addAction(COPY_PASSWORD);
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 com.keepassdroid.app.App;
import com.keepassdroid.intents.Intents;
import com.keepassdroid.timeout.Timeout;
public class TimeoutService extends Service {
private static final String TAG = "KeePassDroid Timer";
@@ -44,42 +44,36 @@ public class TimeoutService extends Service {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ( action.equals(Intents.TIMEOUT) ) {
timeout(context);
if ( action != null && action.equals(Timeout.TIMEOUT) ) {
timeout();
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(Intents.TIMEOUT);
filter.addAction(Timeout.TIMEOUT);
registerReceiver(mIntentReceiver, filter);
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d(TAG, "Timeout service started");
}
private void timeout(Context context) {
private void timeout() {
Log.d(TAG, "Timeout");
App.setShutdown();
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (nm != null)
nm.cancelAll();
stopSelf();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "Timeout service stopped");
unregisterReceiver(mIntentReceiver);
}

View File

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

View File

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