Skip to content

Commit

Permalink
Migrate to Null Safety and remove usage of deprecated code
Browse files Browse the repository at this point in the history
  • Loading branch information
amitkhairnar44 committed May 18, 2021
1 parent 24c8a89 commit a4c7d15
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 39 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![pub package](https://img.shields.io/pub/v/root_access.svg)](https://pub.dartlang.org/packages/root_access)

# Root access Plugin
A Flutter plugin for getting root access in Android apps.
A Flutter plugin for requesting root access in Android apps.

#### This plugin is based on [RootTools](https://github.com/Stericson/RootTools) by [Stericson](https://github.com/Stericson).

Expand All @@ -24,13 +24,13 @@ bool _rootStatus = false;

The ```RootAccess``` class provides a getter to get root access and status
```dart
bool rootStatus = await RootAccess.rootAccess;
bool rootStatus = await RootAccess.requestRootAccess;
```

Declare an async method to retrieve root access
```dart
Future<void> initRootRequest() async {
bool rootStatus = await RootAccess.rootAccess;
bool rootStatus = await RootAccess.requestRootAccess;
setState(() {
_rootStatus = rootStatus;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,14 @@
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.PluginRegistry.Registrar;

/**
* RootAccessPlugin
*/
public class RootAccessPlugin implements FlutterPlugin, MethodCallHandler {
/**
* Plugin registration.
*/

private MethodChannel channel;

public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "root_access");
channel.setMethodCallHandler(new RootAccessPlugin());
}

@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("isAccessGiven")) {
Expand All @@ -41,7 +32,7 @@ private boolean isAccessGiven() {

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "root_access");
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "root_access");
channel.setMethodCallHandler(this);
}

Expand Down
4 changes: 3 additions & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:label="root_access_example"
android:icon="@mipmap/ic_launcher">
<activity
Expand All @@ -35,5 +34,8 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package com.amitkhairnar.rootaccessexample;

import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.embedding.android.FlutterActivity;

public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
}
}
public class MainActivity extends FlutterActivity { }
12 changes: 6 additions & 6 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class _MyAppState extends State<MyApp> {
}

Future<void> initRootRequest() async {
bool rootAccess = await RootAccess.rootAccess;
bool rootAccess = await RootAccess.requestRootAccess;

// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
Expand All @@ -34,13 +34,13 @@ class _MyAppState extends State<MyApp> {

@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: new Center(
child: new Text('Root access granted: $_rootAccess\n'),
body: Center(
child: Text('Root access granted: $_rootAccess\n'),
),
),
);
Expand Down
3 changes: 3 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: root_access_example
description: Demonstrates how to use the root_access plugin.

environment:
sdk: ">=2.12.0 <3.0.0"

dependencies:
flutter:
sdk: flutter
Expand Down
2 changes: 1 addition & 1 deletion example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void main() {
expect(
find.byWidgetPredicate(
(Widget widget) =>
widget is Text && widget.data.startsWith('Root access granted:'),
widget is Text && widget.data!.startsWith('Root access granted:'),
),
findsOneWidget);
});
Expand Down
14 changes: 10 additions & 4 deletions lib/root_access.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import 'package:flutter/services.dart';
/// Provides a way to get root access.
class RootAccess {
/// Channel used to communicate to native code.
static const MethodChannel channel = const MethodChannel('root_access');
static const MethodChannel _channel = const MethodChannel('root_access');

/// Triggers app to get root access
@Deprecated("Use requestRootAccess instead")
static Future<bool> get rootAccess async {
final bool access = await _channel.invokeMethod('isAccessGiven');
return access;
}

/// Triggers app to request root access
///
/// If device is not rooted then this will return `false`.
/// Else it will open installed superuser or similar app's pop up asking for Permission.
static Future<bool> get rootAccess async {
final bool access = await channel.invokeMethod('isAccessGiven');
static Future<bool> get requestRootAccess async {
final bool access = await _channel.invokeMethod('isAccessGiven');
return access;
}
}
8 changes: 4 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: root_access
description: Flutter plugin to get root access in Android device. This plugin is Android exclusive only.
version: 1.0.3
description: Flutter plugin to request root access in Android device. This plugin is Android exclusive only.
version: 1.0.4
homepage: https://github.com/amitkhairnar44/root_access

environment:
sdk: ">=2.7.0 <3.0.0"
flutter: ">=1.10.0"
sdk: ">=2.12.0 <3.0.0"
flutter: ">=2.0.0"

dependencies:
flutter:
Expand Down
23 changes: 23 additions & 0 deletions test/root_access_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:root_access/root_access.dart';

void main() {
const MethodChannel channel = MethodChannel('root_access');

TestWidgetsFlutterBinding.ensureInitialized();

setUp(() {
channel.setMockMethodCallHandler((MethodCall methodCall) async {
return false;
});
});

tearDown(() {
channel.setMockMethodCallHandler(null);
});

test('isAccessGiven', () async {
expect(await RootAccess.requestRootAccess, false);
});
}

0 comments on commit a4c7d15

Please sign in to comment.