hide app thumbnails setting

This commit is contained in:
Adam Velebil 2022-09-07 19:28:22 +02:00
parent 274bcaba6e
commit b1ec64689a
No known key found for this signature in database
GPG Key ID: AC6D6B9D715FC084
3 changed files with 64 additions and 7 deletions

View File

@ -23,6 +23,8 @@ import com.yubico.yubikit.core.Logger
import com.yubico.yubikit.core.YubiKeyDevice
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodChannel
import kotlinx.coroutines.launch
import java.io.Closeable
import java.util.concurrent.Executors
@ -45,17 +47,11 @@ class MainActivity : FlutterFragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// if (!BuildConfig.DEBUG) {
// window.setFlags(
// WindowManager.LayoutParams.FLAG_SECURE,
// WindowManager.LayoutParams.FLAG_SECURE
// )
// }
yubikit = YubiKitManager(this)
setupYubiKeyDiscovery()
setupYubiKitLogger()
hideAppThumbnail(true)
}
override fun onNewIntent(intent: Intent) {
@ -170,6 +166,7 @@ class MainActivity : FlutterFragmentActivity() {
private lateinit var appPreferences: AppPreferences
private lateinit var flutterLog: FlutterLog
private lateinit var flutterStreams: List<Closeable>
private lateinit var appMethodChannel: AppMethodChannel
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
@ -180,6 +177,7 @@ class MainActivity : FlutterFragmentActivity() {
appContext = AppContext(messenger, this.lifecycleScope, viewModel)
dialogManager = DialogManager(messenger, this.lifecycleScope)
appPreferences = AppPreferences(this)
appMethodChannel = AppMethodChannel(messenger)
flutterStreams = listOf(
viewModel.deviceInfo.streamTo(this, messenger, "android.devices.deviceInfo"),
@ -204,6 +202,7 @@ class MainActivity : FlutterFragmentActivity() {
companion object {
const val TAG = "MainActivity"
const val FLAG_SECURE = WindowManager.LayoutParams.FLAG_SECURE
}
/** We observed that some devices (Pixel 2, OnePlus 6) automatically end NFC discovery
@ -219,4 +218,35 @@ class MainActivity : FlutterFragmentActivity() {
(context as? MainActivity)?.startNfcDiscovery()
}
}
inner class AppMethodChannel(messenger: BinaryMessenger) {
private val methodChannel = MethodChannel(messenger, "app.methods")
init {
methodChannel.setMethodCallHandler { methodCall, result ->
when (methodCall.method) {
"hideAppThumbnail" -> result.success(
hideAppThumbnail(
methodCall.arguments as Boolean,
)
)
else -> Log.w(TAG, "Unknown app method: ${methodCall.method}")
}
}
}
}
private fun hideAppThumbnail(value: Boolean): Boolean {
if (value) {
Log.d(TAG, "Setting FLAG_SECURE (hideAppThumbnail)")
window.setFlags(FLAG_SECURE, FLAG_SECURE)
} else {
Log.d(TAG, "Clearing FLAG_SECURE (hideAppThumbnail)")
window.clearFlags(FLAG_SECURE)
}
return FLAG_SECURE == (window.attributes.flags and FLAG_SECURE)
}
}

View File

@ -0,0 +1,4 @@
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
final appMethodsProvider = Provider<MethodChannel>( (ref) => const MethodChannel('app.methods'));

View File

@ -1,11 +1,17 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:yubico_authenticator/android/app_methods.dart';
import 'package:yubico_authenticator/app/logging.dart';
import 'package:yubico_authenticator/core/state.dart';
import '../../app/state.dart';
import '../../widgets/list_title.dart';
import '../../widgets/responsive_dialog.dart';
final _log = Logger('android_settings_page');
final _hideAppThumbnailProvider = StateProvider<bool>((ref) => true);
class AndroidSettingsPage extends ConsumerWidget {
const AndroidSettingsPage({super.key});
@ -26,6 +32,8 @@ class AndroidSettingsPage extends ConsumerWidget {
ref.watch(prefProvider).getString(prefClipKbdLayout) ??
defaultClipKbdLayout;
final themeMode = ref.watch(themeModeProvider);
final flatSecure = ref.watch(_hideAppThumbnailProvider);
return ResponsiveDialog(
title: const Text('Settings'),
child: Column(
@ -77,6 +85,21 @@ class AndroidSettingsPage extends ConsumerWidget {
ref.read(themeModeProvider.notifier).setThemeMode(newMode);
},
),
const ListTitle('Security'),
SwitchListTile(
title: const Text('Hide app thumbnail'),
value: flatSecure,
onChanged: (value) async {
try {
bool hideAppThumbnail = await ref
.read(appMethodsProvider)
.invokeMethod('hideAppThumbnail', value);
ref.read(_hideAppThumbnailProvider.notifier).state =
hideAppThumbnail;
} catch (e) {
_log.error('Failed to call hideAppThumbnail', e);
}
}),
],
),
);