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 Mar 20, 2017
1 parent edd16c7 commit 80c13b6
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 18 deletions.
13 changes: 11 additions & 2 deletions Media/Video/app/build.gradle
@@ -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'
}
28 changes: 20 additions & 8 deletions Media/Video/app/src/main/AndroidManifest.xml
@@ -1,13 +1,25 @@
<?xml version="1.0"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.commonsware.android.video" android:versionCode="1" android:versionName="1.0">

<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="6"/>
<supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="false"/>
<application android:icon="@drawable/ic_launcher">
<activity android:label="VideoDemo" android:name=".VideoDemo">
<manifest package="com.commonsware.android.video"
xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0">

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

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

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".VideoDemo"
android:label="VideoDemo">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Expand Down
@@ -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()]));
}
}
Expand Up @@ -15,20 +15,34 @@
package com.commonsware.android.video;

import java.io.File;
import android.Manifest;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Environment;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class VideoDemo extends Activity {
public class VideoDemo extends AbstractPermissionActivity {
private VideoView video;
private MediaController ctlr;


@Override
protected String[] getDesiredPermissions() {
return(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE});
}

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

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
public void onReady(Bundle icicle) {
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.main);

Expand Down
3 changes: 2 additions & 1 deletion Media/Video/app/src/main/res/values/strings.xml
@@ -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>
2 changes: 1 addition & 1 deletion Media/Video/build.gradle
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'com.android.tools.build:gradle:2.3.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions Media/Video/gradle/wrapper/gradle-wrapper.properties
@@ -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

0 comments on commit 80c13b6

Please sign in to comment.