Skip to content

Commit

Permalink
Merge pull request #204 from keshav-space/203-secure-native
Browse files Browse the repository at this point in the history
Refactor(android): Use native API for secure display
  • Loading branch information
keshav-space authored Jun 7, 2024
2 parents e5f920b + 2832fd0 commit 092fdbd
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 20 deletions.
6 changes: 0 additions & 6 deletions android/app/src/main/kotlin/com/example/notes/MainActivity.kt

This file was deleted.

73 changes: 73 additions & 0 deletions android/app/src/main/kotlin/com/trisven/safenotes/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (C) Keshav Priyadarshi and others - All Rights Reserved.
*
* SPDX-License-Identifier: GPL-3.0-or-later
* You may use, distribute and modify this code under the
* terms of the GPL-3.0+ license.
*
* You should have received a copy of the GNU General Public License v3.0 with
* this file. If not, please visit https://www.gnu.org/licenses/gpl-3.0.html
*
* See https://safenotes.dev for support or download.
*/

package com.trisven.safenotes

import android.content.SharedPreferences
import android.os.Build
import android.os.Bundle
import android.view.WindowManager
import io.flutter.embedding.android.FlutterFragmentActivity

class MainActivity : FlutterFragmentActivity() {

private val PREFS_NAME = "FlutterSharedPreferences"
private val SECURE_FLAG_KEY = "flutter.isFlagSecure"

private val prefsListener =
SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->
if (key == SECURE_FLAG_KEY) {
updateSecureDisplaySetting()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
// Initial secure display setting
updateSecureDisplaySetting()

// Listen for changes in the secure display setting
val prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
prefs.registerOnSharedPreferenceChangeListener(prefsListener)
super.onCreate(savedInstanceState)
}

override fun onDestroy() {
val prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
prefs.unregisterOnSharedPreferenceChangeListener(prefsListener)
super.onDestroy()
}

private fun updateSecureDisplaySetting() {
val prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
val isSecure = prefs.getBoolean(SECURE_FLAG_KEY, true)

if (isSecure) {
window.setFlags(
WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE
)

} else {
window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}


// Disable screenshots in recents screen if API level is 33 or higher
// https://developer.android.com/reference/android/app/Activity#setRecentsScreenshotEnabled(boolean)

// Note: App screenshots will never appear in recent screens even if secure display is turned off
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
setRecentsScreenshotEnabled(false)
}
}
}
5 changes: 0 additions & 5 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@

// Dart imports:
import 'dart:async';
import 'dart:io' show Platform;

// Flutter imports:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

// Package imports:
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter_windowmanager/flutter_windowmanager.dart';
import 'package:local_session_timeout/local_session_timeout.dart';

// Project imports:
Expand All @@ -43,9 +41,6 @@ Future main() async {
));

await PreferencesStorage.init();
if (Platform.isAndroid && PreferencesStorage.isFlagSecure) {
await FlutterWindowManager.addFlags(FlutterWindowManager.FLAG_SECURE);
}

await SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
Expand Down
8 changes: 0 additions & 8 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,6 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_windowmanager:
dependency: "direct main"
description:
name: flutter_windowmanager
sha256: b4d0bc06f6777952b729c0cdb7ce9ad1ecabd8b8b1cb0acb57a36621457dab1b
url: "https://pub.dev"
source: hosted
version: "0.2.0"
glob:
dependency: transitive
description:
Expand Down
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ dependencies:
file_picker: ^8.0.0+1
flutter_secure_storage: ^9.0.0
flutter_staggered_grid_view: ^0.7.0
flutter_windowmanager: ^0.2.0
intl: ^0.18.1
local_auth: ^2.1.2
local_session_timeout: ^2.1.1
Expand Down

0 comments on commit 092fdbd

Please sign in to comment.