Skip to content

Commit

Permalink
- Implement Android native side
Browse files Browse the repository at this point in the history
- Implement iOS native side
  • Loading branch information
skanderhamdi committed Feb 7, 2024
1 parent 32327ac commit 2761c5a
Show file tree
Hide file tree
Showing 20 changed files with 362 additions and 435 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,108 @@
package dz.alex.screen_protection.screen_protection

/** Needed Android libraries **/
import android.app.Activity
import android.util.Log
import androidx.annotation.NonNull
import android.view.WindowManager

/** Flutter related **/
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.android.FlutterActivity
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding

/** ScreenProtectionPlugin */
class ScreenProtectionPlugin: FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
class ScreenProtectionPlugin: FlutterPlugin, ActivityAware {

private lateinit var channel : MethodChannel
private var pluginBinding: FlutterPlugin.FlutterPluginBinding? = null
private var activity: FlutterActivity? = null
private var binaryMessenger: BinaryMessenger? = null

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity as FlutterActivity
Log.d(TAG, "onAttachedToActivity: channel.setMethodCallHandler")
channel.setMethodCallHandler { call, result ->
when(call.method) {
"secureScreen" -> {
secureScreen()
result.success(true)
}
"unsecureScreen" -> {
unsecureScreen()
result.success(true)
}
"toggleScreenSecurity" -> {
toggleScreenSecurity()
result.success(true)
}
"isScreenSecured" -> {
result.success(isScreenSecured())
}
else -> result.notImplemented()
}
}
}

override fun onDetachedFromActivityForConfigChanges() {}

override fun onDetachedFromActivity() {}

override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {}

override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "screen_protection")
channel.setMethodCallHandler(this)
binaryMessenger = flutterPluginBinding.binaryMessenger
}

override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}

/*override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
override fun onMethodCall(call: MethodCall, result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
} else {
result.notImplemented()
when(call.method) {
"secureScreen" -> {
secureScreen()
result.success(true)
}
"unsecureScreen" -> {
unsecureScreen()
result.success(true)
}
"toggleScreenSecurity" -> {
toggleScreenSecurity()
result.success(true)
}
"isScreenSecured" -> {
result.success(isScreenSecured())
}
else -> result.notImplemented()
}
}*/

fun secureScreen() {
activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
}

override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
fun unsecureScreen() {
activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}

fun toggleScreenSecurity() {
val isFlagSecureSet = (activity?.window?.attributes?.flags ?: 0) and WindowManager.LayoutParams.FLAG_SECURE != 0
if(isFlagSecureSet) unsecureScreen() else secureScreen()
}

fun isScreenSecured(): Boolean {
return (activity?.window?.attributes?.flags ?: 0) and WindowManager.LayoutParams.FLAG_SECURE != 0
}
}
25 changes: 0 additions & 25 deletions example/integration_test/plugin_integration_test.dart

This file was deleted.

1 change: 1 addition & 0 deletions example/ios/Flutter/Debug.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
#include "Generated.xcconfig"
1 change: 1 addition & 0 deletions example/ios/Flutter/Release.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
#include "Generated.xcconfig"
40 changes: 40 additions & 0 deletions example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Uncomment this line to define a global platform for your project
# platform :ios, '11.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

project 'Runner', {
'Debug' => :debug,
'Profile' => :release,
'Release' => :release,
}

def flutter_root
generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
unless File.exist?(generated_xcode_build_settings_path)
raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
end

File.foreach(generated_xcode_build_settings_path) do |line|
matches = line.match(/FLUTTER_ROOT\=(.*)/)
return matches[1].strip if matches
end
raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end

require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

flutter_ios_podfile_setup

target 'Runner' do
use_frameworks!
use_modular_headers!
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end

post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
end
end
22 changes: 22 additions & 0 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
PODS:
- Flutter (1.0.0)
- screen_protection (0.0.1):
- Flutter

DEPENDENCIES:
- Flutter (from `Flutter`)
- screen_protection (from `.symlinks/plugins/screen_protection/ios`)

EXTERNAL SOURCES:
Flutter:
:path: Flutter
screen_protection:
:path: ".symlinks/plugins/screen_protection/ios"

SPEC CHECKSUMS:
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
screen_protection: f94a6e8a06598ea276003a5cf6b4bcd641bd7802

PODFILE CHECKSUM: bfd8cb9c5a07b05028ccd5e7d27153963d4bba14

COCOAPODS: 1.12.0
Loading

0 comments on commit 2761c5a

Please sign in to comment.