2019-02-07 22:39:33 +01:00
|
|
|
package com.beemdevelopment.aegis;
|
2017-12-23 22:33:32 +01:00
|
|
|
|
|
|
|
import android.app.Application;
|
2019-06-12 01:49:26 +02:00
|
|
|
import android.app.NotificationChannel;
|
|
|
|
import android.app.NotificationManager;
|
2019-04-07 18:18:50 +02:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
2017-12-26 13:04:26 +01:00
|
|
|
import android.content.Intent;
|
2019-04-07 18:18:50 +02:00
|
|
|
import android.content.IntentFilter;
|
2017-12-26 13:04:26 +01:00
|
|
|
import android.content.pm.ShortcutInfo;
|
|
|
|
import android.content.pm.ShortcutManager;
|
|
|
|
import android.graphics.drawable.Icon;
|
|
|
|
import android.os.Build;
|
2019-02-07 22:39:33 +01:00
|
|
|
|
|
|
|
import com.beemdevelopment.aegis.db.DatabaseManager;
|
2019-06-12 01:49:26 +02:00
|
|
|
import com.beemdevelopment.aegis.services.NotificationService;
|
2019-02-07 22:39:33 +01:00
|
|
|
import com.beemdevelopment.aegis.ui.MainActivity;
|
2019-09-14 15:20:02 +02:00
|
|
|
import com.mikepenz.iconics.Iconics;
|
|
|
|
import com.mikepenz.material_design_iconic_typeface_library.MaterialDesignIconic;
|
2019-02-07 22:39:33 +01:00
|
|
|
|
2019-05-12 17:12:09 +02:00
|
|
|
import java.util.ArrayList;
|
2017-12-26 13:04:26 +01:00
|
|
|
import java.util.Collections;
|
2019-05-12 17:12:09 +02:00
|
|
|
import java.util.List;
|
2017-12-23 22:33:32 +01:00
|
|
|
|
2019-04-04 14:07:36 +02:00
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
|
2017-12-23 22:33:32 +01:00
|
|
|
public class AegisApplication extends Application {
|
2018-05-11 21:29:10 +02:00
|
|
|
private DatabaseManager _manager;
|
|
|
|
private Preferences _prefs;
|
2019-05-12 17:12:09 +02:00
|
|
|
private List<LockListener> _lockListeners;
|
2017-12-23 22:33:32 +01:00
|
|
|
|
2019-06-12 01:49:26 +02:00
|
|
|
private static final String CODE_LOCK_STATUS_ID = "lock_status_channel";
|
|
|
|
private static final String CODE_LOCK_DATABASE_ACTION = "lock_database";
|
|
|
|
|
2017-12-23 22:33:32 +01:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
2018-05-11 21:29:10 +02:00
|
|
|
_manager = new DatabaseManager(this);
|
|
|
|
_prefs = new Preferences(this);
|
2019-05-12 17:12:09 +02:00
|
|
|
_lockListeners = new ArrayList<>();
|
2017-12-26 13:04:26 +01:00
|
|
|
|
2019-09-14 15:20:02 +02:00
|
|
|
Iconics.init(this);
|
|
|
|
Iconics.registerFont(new MaterialDesignIconic());
|
|
|
|
|
2019-04-07 18:18:50 +02:00
|
|
|
// listen for SCREEN_OFF events
|
|
|
|
ScreenOffReceiver receiver = new ScreenOffReceiver();
|
2019-06-12 01:49:26 +02:00
|
|
|
IntentFilter intentFilter = new IntentFilter();
|
|
|
|
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
|
|
|
|
intentFilter.addAction(CODE_LOCK_DATABASE_ACTION);
|
|
|
|
registerReceiver(receiver, intentFilter);
|
2019-04-07 18:18:50 +02:00
|
|
|
|
2017-12-26 13:04:26 +01:00
|
|
|
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
|
|
|
initAppShortcuts();
|
|
|
|
}
|
2019-06-12 01:49:26 +02:00
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
initNotificationChannels();
|
|
|
|
}
|
2017-12-23 22:33:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public DatabaseManager getDatabaseManager() {
|
|
|
|
return _manager;
|
|
|
|
}
|
|
|
|
|
2018-05-11 20:08:51 +02:00
|
|
|
public Preferences getPreferences() {
|
|
|
|
return _prefs;
|
2017-12-23 22:33:32 +01:00
|
|
|
}
|
|
|
|
|
2019-05-12 17:12:09 +02:00
|
|
|
public boolean isAutoLockEnabled() {
|
|
|
|
return _prefs.isAutoLockEnabled() && _manager.isLoaded() && _manager.isEncryptionEnabled() && !_manager.isLocked();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void registerLockListener(LockListener listener) {
|
|
|
|
_lockListeners.add(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void unregisterLockListener(LockListener listener) {
|
|
|
|
_lockListeners.remove(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void lock() {
|
|
|
|
_manager.lock();
|
|
|
|
for (LockListener listener : _lockListeners) {
|
|
|
|
listener.onLocked();
|
|
|
|
}
|
2019-06-12 01:49:26 +02:00
|
|
|
|
|
|
|
stopService(new Intent(AegisApplication.this, NotificationService.class));
|
2019-05-12 17:12:09 +02:00
|
|
|
}
|
|
|
|
|
2017-12-26 13:04:26 +01:00
|
|
|
@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")
|
2018-10-09 23:13:51 +02:00
|
|
|
.setShortLabel(getString(R.string.new_profile))
|
|
|
|
.setLongLabel(getString(R.string.add_new_profile))
|
2019-05-29 23:36:52 +02:00
|
|
|
.setIcon(Icon.createWithResource(this, R.drawable.ic_qr_code))
|
2017-12-26 13:04:26 +01:00
|
|
|
.setIntent(intent)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut));
|
|
|
|
}
|
2019-04-07 18:18:50 +02:00
|
|
|
|
2019-06-12 01:49:26 +02:00
|
|
|
private void initNotificationChannels() {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
CharSequence name = getString(R.string.channel_name_lock_status);
|
|
|
|
String description = getString(R.string.channel_description_lock_status);
|
|
|
|
int importance = NotificationManager.IMPORTANCE_LOW;
|
|
|
|
|
|
|
|
NotificationChannel channel = new NotificationChannel(CODE_LOCK_STATUS_ID, name, importance);
|
|
|
|
channel.setDescription(description);
|
|
|
|
|
|
|
|
NotificationManager notificationManager = getSystemService(NotificationManager.class);
|
|
|
|
notificationManager.createNotificationChannel(channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ScreenOffReceiver extends BroadcastReceiver {
|
2019-04-07 18:18:50 +02:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
if (isAutoLockEnabled()) {
|
2019-05-12 17:12:09 +02:00
|
|
|
lock();
|
2019-04-07 18:18:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-12 17:12:09 +02:00
|
|
|
|
|
|
|
public interface LockListener {
|
|
|
|
void onLocked();
|
|
|
|
}
|
2017-12-23 22:33:32 +01:00
|
|
|
}
|