Add receiver to lock the entry in the keyboard

This commit is contained in:
J-Jamet
2018-06-29 17:59:58 +02:00
parent 9855ae79c3
commit 3674900a54
14 changed files with 133 additions and 143 deletions

View File

@@ -33,6 +33,7 @@ public class EntrySelectionHelper {
Log.d(activity.getClass().getName(), "Reply entry selection");
Entry entryModel = new Entry();
entryModel.setTitle(entry.getTitle());
entryModel.setUsername(entry.getUsername());
entryModel.setPassword(entry.getPassword());
entryModel.setUrl(entry.getUrl());

View File

@@ -60,7 +60,7 @@ import com.kunzisoft.keepass.settings.preferenceDialogFragment.MemoryUsagePrefer
import com.kunzisoft.keepass.settings.preferenceDialogFragment.ParallelismPreferenceDialogFragmentCompat;
import com.kunzisoft.keepass.settings.preferenceDialogFragment.RoundsPreferenceDialogFragmentCompat;
import com.kunzisoft.keepass.stylish.Stylish;
import com.kunzisoft.magikeyboard.MagikIMESettings;
import com.kunzisoft.magikeyboard.settings.MagikIMESettings;
public class NestedSettingsFragment extends PreferenceFragmentCompat
implements Preference.OnPreferenceClickListener {

View File

@@ -9,12 +9,14 @@ import java.util.Set;
public class Entry implements Parcelable {
private String title;
private String username;
private String password;
private String url;
private Map<String, String> customFields;
public Entry() {
this.title = "";
this.username = "";
this.password = "";
this.url = "";
@@ -22,6 +24,7 @@ public class Entry implements Parcelable {
}
protected Entry(Parcel in) {
title = in.readString();
username = in.readString();
password = in.readString();
url = in.readString();
@@ -41,6 +44,14 @@ public class Entry implements Parcelable {
}
};
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUsername() {
return username;
}
@@ -84,6 +95,7 @@ public class Entry implements Parcelable {
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(title);
parcel.writeString(username);
parcel.writeString(password);
parcel.writeString(url);

View File

@@ -14,12 +14,11 @@
</service>
<service android:name=".KeyboardEntryNotificationService" />
<receiver
android:name=".KeyboardEntryNotificationService$NotificationDeleteBroadcastReceiver"
android:name=".receiver.NotificationDeleteBroadcastReceiver"
android:exported="false" >
</receiver>
<activity android:name=".KeyboardManagerActivity"
android:label=""/>
<activity android:name="com.kunzisoft.magikeyboard.MagikIMESettings"
<activity android:name="com.kunzisoft.magikeyboard.settings.MagikIMESettings"
android:label="@string/keyboard_setting_label">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

View File

@@ -4,16 +4,19 @@ import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.IntentFilter;
import android.os.Build;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.kunzisoft.magikeyboard.receiver.LockBroadcastReceiver;
import com.kunzisoft.magikeyboard.receiver.NotificationDeleteBroadcastReceiver;
import static android.content.ContentValues.TAG;
import static com.kunzisoft.magikeyboard.receiver.LockBroadcastReceiver.LOCK_ACTION;
public class KeyboardEntryNotificationService extends Service {
@@ -45,6 +48,14 @@ public class KeyboardEntryNotificationService extends Service {
NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(channel);
}
// Register a lock receiver to stop notification service when lock on keyboard is performed
registerReceiver(new LockBroadcastReceiver() {
@Override
public void onReceiveLock(Context context, Intent intent) {
context.stopService(new Intent(context, KeyboardEntryNotificationService.class));
}
}, new IntentFilter(LOCK_ACTION));
}
@Override
@@ -60,28 +71,31 @@ public class KeyboardEntryNotificationService extends Service {
private void newNotification() {
Intent contentIntent = new Intent(this, KeyboardManagerActivity.class);
contentIntent.setAction(Intent.ACTION_MAIN);
contentIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingContentIntent = PendingIntent.getActivity(
this, 0, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent deleteIntent = new Intent(this, NotificationDeleteBroadcastReceiver.class);
PendingIntent pendingDeleteIntent =
PendingIntent.getBroadcast(getApplicationContext(), 0, deleteIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID_KEYBOARD)
.setSmallIcon(R.drawable.ic_vpn_key_white_24dp)
.setContentTitle(getString(R.string.notification_entry_content_title, "Entry"))
.setAutoCancel(false)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
.setContentText(getString(R.string.notification_entry_content_text, "Username"))
.setContentIntent(pendingContentIntent)
.setDeleteIntent(pendingDeleteIntent);
if (MagikIME.getEntryKey() != null) {
String entryTitle = getString(R.string.notification_entry_content_title_text);
String entryUsername = "";
if (!MagikIME.getEntryKey().getTitle().isEmpty())
entryTitle = MagikIME.getEntryKey().getTitle();
if (!MagikIME.getEntryKey().getUsername().isEmpty())
entryUsername = MagikIME.getEntryKey().getUsername();
notificationManager.cancel(notificationId);
notificationManager.notify(notificationId, builder.build());
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID_KEYBOARD)
.setSmallIcon(R.drawable.ic_vpn_key_white_24dp)
.setContentTitle(getString(R.string.notification_entry_content_title, entryTitle))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
.setContentText(getString(R.string.notification_entry_content_text, entryUsername))
.setAutoCancel(false)
.setContentIntent(null)
.setDeleteIntent(pendingDeleteIntent);
notificationManager.cancel(notificationId);
notificationManager.notify(notificationId, builder.build());
}
// TODO Get timeout
/*
@@ -118,22 +132,4 @@ public class KeyboardEntryNotificationService extends Service {
notificationManager.cancel(notificationId);
}
public class NotificationDeleteBroadcastReceiver extends BroadcastReceiver {
// TODO Crash here
@Override
public void onReceive(Context context, Intent intent) {
// Clear the entry if define in preferences
SharedPreferences sharedPreferences = android.preference.PreferenceManager.getDefaultSharedPreferences(context);
if (sharedPreferences.getBoolean(getString(R.string.notification_entry_clear_close_key),
getResources().getBoolean(R.bool.notification_entry_clear_close_default))) {
MagikIME.deleteEntryKey(context);
}
// Stop the service in all cases
stopSelf();
}
}
}

View File

@@ -1,62 +0,0 @@
package com.kunzisoft.magikeyboard;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
/**
* Activity to show keyboard manager
*/
public class KeyboardManagerActivity extends AppCompatActivity {
private View rootView;
enum DialogState {
PICKING, CHOSEN
}
private DialogState mState;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.empty);
rootView = findViewById(R.id.root_view);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(mState == DialogState.PICKING) {
mState = DialogState.CHOSEN;
}
else if(mState == DialogState.CHOSEN) {
finish();
}
}
@Override
protected void onResume() {
super.onResume();
rootView.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imeManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (imeManager != null) {
imeManager.showInputMethodPicker();
}
mState = DialogState.PICKING;
}
}, 100);
}
@Override
public void onBackPressed() {
super.onBackPressed();
// Close the back activity
finish();
}
}

View File

@@ -19,9 +19,9 @@
*/
package com.kunzisoft.magikeyboard;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
@@ -37,14 +37,14 @@ import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
import com.kunzisoft.keepass_model.Entry;
import com.kunzisoft.magikeyboard.utils.Utility;
import com.kunzisoft.magikeyboard.receiver.LockBroadcastReceiver;
import static com.kunzisoft.magikeyboard.receiver.LockBroadcastReceiver.LOCK_ACTION;
public class MagikIME extends InputMethodService
implements KeyboardView.OnKeyboardActionListener {
private static final String TAG = MagikIME.class.getName();
private static final String LOCK_ACTION = "com.kunzisoft.keepass.LOCK";
private static final int KEY_CHANGE_KEYBOARD = 600;
private static final int KEY_UNLOCK = 610;
private static final int KEY_LOCK = 611;
@@ -60,6 +60,20 @@ public class MagikIME extends InputMethodService
private Keyboard keyboard;
private Keyboard keyboard_entry;
@Override
public void onCreate() {
super.onCreate();
// Remove the entry and lock the keyboard when the lock signal is receive
registerReceiver(new LockBroadcastReceiver() {
@Override
public void onReceiveLock(Context context, Intent intent) {
entryKey = null;
assignKeyboardView();
}
}, new IntentFilter(LOCK_ACTION));
}
@Override
public View onCreateInputView() {
keyboardView = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard, null);
@@ -88,6 +102,10 @@ public class MagikIME extends InputMethodService
assignKeyboardView();
}
public static Entry getEntryKey() {
return entryKey;
}
public static void setEntryKey(Entry entry) {
entryKey = entry;
}
@@ -110,7 +128,9 @@ public class MagikIME extends InputMethodService
imeManager.switchToLastInputMethod(getWindow().getWindow().getAttributes().token);
} catch (Exception e) {
Log.e(TAG, "Unable to switch to the previous IME", e);
Utility.openInputMethodPicker(this);
InputMethodManager imeManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (imeManager != null)
imeManager.showInputMethodPicker();
}
// TODO Add a long press to choose the keyboard
break;
@@ -218,18 +238,4 @@ public class MagikIME extends InputMethodService
default: am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
}
}
public class LockBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null
&& action.equals(LOCK_ACTION)) {
entryKey = null;
assignKeyboardView();
}
}
}
}

View File

@@ -0,0 +1,22 @@
package com.kunzisoft.magikeyboard.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public abstract class LockBroadcastReceiver extends BroadcastReceiver {
public static final String LOCK_ACTION = "com.kunzisoft.keepass.LOCK";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null
&& action.equals(LOCK_ACTION)) {
onReceiveLock(context, intent);
}
}
public abstract void onReceiveLock(Context context, Intent intent);
}

View File

@@ -0,0 +1,27 @@
package com.kunzisoft.magikeyboard.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import com.kunzisoft.magikeyboard.KeyboardEntryNotificationService;
import com.kunzisoft.magikeyboard.MagikIME;
import com.kunzisoft.magikeyboard.R;
public class NotificationDeleteBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Clear the entry if define in preferences
SharedPreferences sharedPreferences = android.preference.PreferenceManager.getDefaultSharedPreferences(context);
if (sharedPreferences.getBoolean(context.getString(R.string.notification_entry_clear_close_key),
context.getResources().getBoolean(R.bool.notification_entry_clear_close_default))) {
MagikIME.deleteEntryKey(context);
}
// Stop the service in all cases
context.stopService(new Intent(context, KeyboardEntryNotificationService.class));
}
}

View File

@@ -17,13 +17,15 @@
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.magikeyboard;
package com.kunzisoft.magikeyboard.settings;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import com.kunzisoft.magikeyboard.R;
public class MagikIMESettings extends AppCompatActivity {
@Override

View File

@@ -1,8 +1,10 @@
package com.kunzisoft.magikeyboard;
package com.kunzisoft.magikeyboard.settings;
import android.os.Bundle;
import android.support.v7.preference.PreferenceFragmentCompat;
import com.kunzisoft.magikeyboard.R;
public class MagikIMESettingsFragment extends PreferenceFragmentCompat {
@Override

View File

@@ -1,17 +0,0 @@
package com.kunzisoft.magikeyboard.utils;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import static android.content.Context.INPUT_METHOD_SERVICE;
public class Utility {
public static void openInputMethodPicker(Context context) {
// TODO Change don't really work on activity
InputMethodManager imeManager = (InputMethodManager) context.getSystemService(INPUT_METHOD_SERVICE);
if (imeManager != null)
imeManager.showInputMethodPicker();
}
}

View File

@@ -32,6 +32,7 @@
<string name="notification_entry_summary">Show a notification when an entry is available</string>
<bool name="notification_entry_default" translatable="false">true</bool>
<string name="notification_entry_content_title_text">Entry</string>
<string name="notification_entry_content_title">%1$s available on Magikeyboard</string>
<string name="notification_entry_content_text">%1$s</string>

View File

@@ -23,7 +23,8 @@
<Preference
android:key="@string/entry_timeout_key"
android:title="@string/entry_timeout_title"
android:summary="@string/entry_timeout_summary" />
android:summary="@string/entry_timeout_summary"
android:enabled="false"/>
<SwitchPreference
android:key="@string/notification_entry_key"
android:title="@string/notification_entry_title"