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
edd16c7
commit 80c13b6
Showing
7 changed files
with
161 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
android { | ||
compileSdkVersion 19 | ||
buildToolsVersion "21.1.2" | ||
compileSdkVersion 25 | ||
buildToolsVersion "25.0.2" | ||
|
||
defaultConfig { | ||
minSdkVersion 15 | ||
targetSdkVersion 25 | ||
} | ||
} | ||
|
||
dependencies { | ||
compile 'com.android.support:support-compat:25.2.0' | ||
} |
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
Media/Video/app/src/main/java/com/commonsware/android/video/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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/*** | ||
Copyright (c) 2015-2017 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. | ||
Covered in detail in the book _The Busy Coder's Guide to Android Development_ | ||
https://commonsware.com/Android | ||
*/ | ||
|
||
package com.commonsware.android.video; | ||
|
||
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); | ||
} | ||
|
||
private 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<string name="app_name">VideoDemo</string> | ||
<string name="app_name">VideoDemo</string> | ||
<string name="msg_sorry">Sorry, but we need permission to continue!</string> | ||
</resources> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#Wed Apr 10 15:27:10 PDT 2013 | ||
#Wed Mar 15 16:02:01 EDT 2017 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip |