Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
885fb84
commit 65791c1
Showing
7 changed files
with
152 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 9 additions & 10 deletions
19
Introspection/ProcessTextBlocker/app/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...ker/app/src/main/java/com/commonsware/android/fileseditor/AbstractPermissionActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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()])); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 7 additions & 8 deletions
15
Introspection/ProcessTextBlocker/app/src/main/res/layout/editor.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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" | /> | ||
/> |
9 changes: 5 additions & 4 deletions
9
Introspection/ProcessTextBlocker/app/src/main/res/values/strings.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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> |