Skip to content

Commit

Permalink
added runtime permission support
Browse files Browse the repository at this point in the history
  • Loading branch information
commonsguy committed Feb 12, 2017
1 parent 885fb84 commit 65791c1
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 31 deletions.
9 changes: 5 additions & 4 deletions Introspection/ProcessTextBlocker/app/build.gradle
Expand Up @@ -2,15 +2,16 @@ apply plugin: 'com.android.application'


dependencies { dependencies {
compile 'io.karim:materialtabs:2.0.2' compile 'io.karim:materialtabs:2.0.2'
compile 'com.android.support:support-v13:22.2.1' compile 'com.android.support:support-v13:25.1.0'
} }


android { android {
compileSdkVersion 22 compileSdkVersion 25
buildToolsVersion "22.0.1" buildToolsVersion "25.0.2"


defaultConfig { defaultConfig {
minSdkVersion 15 minSdkVersion 15
targetSdkVersion 18 targetSdkVersion 25
applicationId "com.commonsware.android.fileseditor.ptb"
} }
} }
19 changes: 9 additions & 10 deletions Introspection/ProcessTextBlocker/app/src/main/AndroidManifest.xml
@@ -1,29 +1,28 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<manifest package="com.commonsware.android.fileseditor" <manifest package="com.commonsware.android.fileseditor"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1" android:versionCode="1"
android:versionName="1.0"> android:versionName="1.0">


<uses-permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


<supports-screens <supports-screens
android:anyDensity="true" android:anyDensity="true"
android:largeScreens="true" android:largeScreens="true"
android:normalScreens="true" android:normalScreens="true"
android:smallScreens="true"/> android:smallScreens="true" />


<application <application
android:icon="@drawable/ic_launcher" android:icon="@drawable/ic_launcher"
android:theme="@style/Theme.Apptheme" android:label="@string/app_name"
android:label="@string/app_name"> android:theme="@style/Theme.Apptheme">
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:label="@string/app_name"> android:label="@string/app_name">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN"/> <action android:name="android.intent.action.MAIN" />


<category android:name="android.intent.category.LAUNCHER"/> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
</application> </application>
Expand Down
@@ -0,0 +1,107 @@
/***
Copyright (c) 2015-2016 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/

package com.commonsware.android.fileseditor;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import java.util.ArrayList;

abstract public class AbstractPermissionActivity
extends Activity {
abstract protected String[] getDesiredPermissions();
abstract protected void onPermissionDenied();
abstract protected void onReady(Bundle state);

private static final int REQUEST_PERMISSION=61125;
private static final String STATE_IN_PERMISSION="inPermission";
private boolean isInPermission=false;
private Bundle state;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.state=savedInstanceState;

if (state!=null) {
isInPermission=state.getBoolean(STATE_IN_PERMISSION, false);
}

if (hasAllPermissions(getDesiredPermissions())) {
onReady(state);
}
else if (!isInPermission) {
isInPermission=true;

ActivityCompat
.requestPermissions(this,
netPermissions(getDesiredPermissions()),
REQUEST_PERMISSION);
}
}

@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions,
int[] grantResults) {
isInPermission=false;

if (requestCode==REQUEST_PERMISSION) {
if (hasAllPermissions(getDesiredPermissions())) {
onReady(state);
}
else {
onPermissionDenied();
}
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

outState.putBoolean(STATE_IN_PERMISSION, isInPermission);
}

private boolean hasAllPermissions(String[] perms) {
for (String perm : perms) {
if (!hasPermission(perm)) {
return(false);
}
}

return(true);
}

protected boolean hasPermission(String perm) {
return(ContextCompat.checkSelfPermission(this, perm)==
PackageManager.PERMISSION_GRANTED);
}

private String[] netPermissions(String[] wanted) {
ArrayList<String> result=new ArrayList<String>();

for (String perm : wanted) {
if (!hasPermission(perm)) {
result.add(perm);
}
}

return(result.toArray(new String[result.size()]));
}
}
@@ -1,5 +1,5 @@
/*** /***
Copyright (c) 2012-2015 CommonsWare, LLC Copyright (c) 2012-2017 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
Expand All @@ -14,15 +14,28 @@


package com.commonsware.android.fileseditor; package com.commonsware.android.fileseditor;


import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.widget.Toast;
import io.karim.MaterialTabs; import io.karim.MaterialTabs;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;


public class MainActivity extends Activity { public class MainActivity extends AbstractPermissionActivity {
@Override @Override
public void onCreate(Bundle savedInstanceState) { protected String[] getDesiredPermissions() {
super.onCreate(savedInstanceState); return(new String[]{WRITE_EXTERNAL_STORAGE});
}

@Override
protected void onPermissionDenied() {
Toast
.makeText(this, R.string.msg_sorry, Toast.LENGTH_LONG)
.show();
finish();
}

@Override
protected void onReady(Bundle savedInstanceState) {
setContentView(R.layout.main); setContentView(R.layout.main);


ViewPager pager=(ViewPager)findViewById(R.id.pager); ViewPager pager=(ViewPager)findViewById(R.id.pager);
Expand Down
Expand Up @@ -17,6 +17,7 @@
import android.app.Fragment; import android.app.Fragment;
import android.app.FragmentManager; import android.app.FragmentManager;
import android.content.Context; import android.content.Context;
import android.os.Build;
import android.os.Environment; import android.os.Environment;
import android.support.v13.app.FragmentPagerAdapter; import android.support.v13.app.FragmentPagerAdapter;
import java.io.File; import java.io.File;
Expand Down
@@ -1,8 +1,7 @@
<EditText <EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/editor" android:id="@+id/editor"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_height="match_parent" android:inputType="textMultiLine"
android:gravity="left|top" android:gravity="left|top"
android:inputType="textMultiLine" />
/>
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="app_name">Process Text Blocker Demo</string> <string name="app_name">Process Text Blocker Demo</string>
<string name="internal">Internal</string> <string name="internal">Internal</string>
<string name="external">External</string> <string name="external">External</string>
<string name="pub">Public</string> <string name="pub">Public</string>
<string name="msg_sorry">Sorry, but we need permission to continue!</string>


</resources> </resources>

0 comments on commit 65791c1

Please sign in to comment.