Skip to content

Commit

Permalink
updated for runtime permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
commonsguy committed Aug 7, 2017
1 parent e7fd0ad commit 5d3838a
Show file tree
Hide file tree
Showing 24 changed files with 364 additions and 44 deletions.
13 changes: 11 additions & 2 deletions Contacts/CallLog/app/build.gradle
@@ -1,6 +1,15 @@
apply plugin: 'com.android.application' apply plugin: 'com.android.application'


android { android {
compileSdkVersion 19 compileSdkVersion 26
buildToolsVersion "25.0.3" buildToolsVersion "26.0.1"

defaultConfig {
minSdkVersion 16
targetSdkVersion 26
}
} }

dependencies {
compile 'com.android.support:support-compat:26.0.0'
}
9 changes: 3 additions & 6 deletions Contacts/CallLog/app/src/main/AndroidManifest.xml
Expand Up @@ -4,15 +4,12 @@
android:versionCode="1" android:versionCode="1"
android:versionName="1.0"> android:versionName="1.0">


<uses-sdk <uses-permission android:name="android.permission.READ_CALL_LOG" />
android:minSdkVersion="11"
android:targetSdkVersion="11"/>

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


<application <application
android:icon="@drawable/ic_launcher" android:icon="@drawable/ic_launcher"
android:label="@string/app_name"> android:label="@string/app_name"
android:theme="@style/Theme.Apptheme">
<activity <activity
android:name=".CallLogConsumerActivity" android:name=".CallLogConsumerActivity"
android:label="@string/app_name"> android:label="@string/app_name">
Expand Down
@@ -0,0 +1,105 @@
/***
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.
Covered in detail in the book _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/

package com.commonsware.android.calllog.consumer;

import android.app.ListActivity;
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 ListActivity {
abstract protected String[] getDesiredPermissions();
abstract protected void onPermissionDenied();
abstract protected void onReady();

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

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

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

if (hasAllPermissions(getDesiredPermissions())) {
onReady();
}
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();
}
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 @@ -14,7 +14,7 @@


package com.commonsware.android.calllog.consumer; package com.commonsware.android.calllog.consumer;


import android.app.ListActivity; import android.Manifest;
import android.app.LoaderManager; import android.app.LoaderManager;
import android.content.CursorLoader; import android.content.CursorLoader;
import android.content.Loader; import android.content.Loader;
Expand All @@ -25,18 +25,31 @@
import android.view.View; import android.view.View;
import android.widget.SimpleCursorAdapter; import android.widget.SimpleCursorAdapter;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;


public class CallLogConsumerActivity extends ListActivity implements public class CallLogConsumerActivity extends AbstractPermissionActivity implements
LoaderManager.LoaderCallbacks<Cursor>, LoaderManager.LoaderCallbacks<Cursor>,
SimpleCursorAdapter.ViewBinder { SimpleCursorAdapter.ViewBinder {
private static final String[] PERMS={Manifest.permission.READ_CALL_LOG};
private static final String[] PROJECTION=new String[] { private static final String[] PROJECTION=new String[] {
CallLog.Calls._ID, CallLog.Calls.NUMBER, CallLog.Calls.DATE }; CallLog.Calls._ID, CallLog.Calls.NUMBER, CallLog.Calls.DATE };
private SimpleCursorAdapter adapter=null; private SimpleCursorAdapter adapter=null;


@Override @Override
public void onCreate(Bundle savedInstanceState) { protected String[] getDesiredPermissions() {
super.onCreate(savedInstanceState); return(PERMS);
}


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

@Override
public void onReady() {
adapter= adapter=
new SimpleCursorAdapter(this, R.layout.row, null, new String[] { new SimpleCursorAdapter(this, R.layout.row, null, new String[] {
CallLog.Calls.NUMBER, CallLog.Calls.DATE }, new int[] { CallLog.Calls.NUMBER, CallLog.Calls.DATE }, new int[] {
Expand Down
9 changes: 9 additions & 0 deletions Contacts/CallLog/app/src/main/res/values-v21/styles.xml
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="Theme.Apptheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
<item name="android:colorPrimary">@color/primary</item>
<item name="android:colorPrimaryDark">@color/primary_dark</item>
<item name="android:colorAccent">@color/accent</item>
</style>
</resources>
6 changes: 6 additions & 0 deletions Contacts/CallLog/app/src/main/res/values/colors.xml
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primary">#3f51b5</color>
<color name="primary_dark">#1a237e</color>
<color name="accent">#ffee58</color>
</resources>
2 changes: 1 addition & 1 deletion Contacts/CallLog/app/src/main/res/values/strings.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="app_name">CallLog Consumer</string> <string name="app_name">CallLog Consumer</string>

<string name="msg_no_perm">Sorry! This demo cannot run without permission!</string>
</resources> </resources>
6 changes: 6 additions & 0 deletions Contacts/CallLog/app/src/main/res/values/styles.xml
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="Theme.Apptheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
</style>
</resources>
3 changes: 2 additions & 1 deletion Contacts/CallLog/build.gradle
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:2.3.2' classpath 'com.android.tools.build:gradle:2.3.3'


// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files
Expand All @@ -15,5 +15,6 @@ buildscript {
allprojects { allprojects {
repositories { repositories {
jcenter() jcenter()
maven { url 'https://maven.google.com' }
} }
} }
9 changes: 7 additions & 2 deletions Contacts/Inserter/app/build.gradle
@@ -1,6 +1,11 @@
apply plugin: 'com.android.application' apply plugin: 'com.android.application'


android { android {
compileSdkVersion 19 compileSdkVersion 26
buildToolsVersion "25.0.3" buildToolsVersion "26.0.1"

defaultConfig {
minSdkVersion 16
targetSdkVersion 26
}
} }
10 changes: 2 additions & 8 deletions Contacts/Inserter/app/src/main/AndroidManifest.xml
Expand Up @@ -4,16 +4,10 @@
android:versionCode="1" android:versionCode="1"
android:versionName="1.0"> android:versionName="1.0">


<uses-sdk
android:minSdkVersion="3"
android:targetSdkVersion="14" />
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="false" />
<application <application
android:icon="@drawable/ic_launcher" android:icon="@drawable/ic_launcher"
android:label="@string/app_name"> android:label="@string/app_name"
android:theme="@style/Theme.Apptheme">
<activity <activity
android:name="ContactsInserter" android:name="ContactsInserter"
android:label="@string/app_name"> android:label="@string/app_name">
Expand Down
9 changes: 9 additions & 0 deletions Contacts/Inserter/app/src/main/res/values-v21/styles.xml
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="Theme.Apptheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
<item name="android:colorPrimary">@color/primary</item>
<item name="android:colorPrimaryDark">@color/primary_dark</item>
<item name="android:colorAccent">@color/accent</item>
</style>
</resources>
6 changes: 6 additions & 0 deletions Contacts/Inserter/app/src/main/res/values/colors.xml
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primary">#3f51b5</color>
<color name="primary_dark">#1a237e</color>
<color name="accent">#ffee58</color>
</resources>
6 changes: 6 additions & 0 deletions Contacts/Inserter/app/src/main/res/values/styles.xml
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="Theme.Apptheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
</style>
</resources>
2 changes: 1 addition & 1 deletion Contacts/Inserter/build.gradle
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:2.3.2' classpath 'com.android.tools.build:gradle:2.3.3'


// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files
Expand Down
13 changes: 11 additions & 2 deletions Contacts/Spinners/app/build.gradle
@@ -1,6 +1,15 @@
apply plugin: 'com.android.application' apply plugin: 'com.android.application'


android { android {
compileSdkVersion 19 compileSdkVersion 26
buildToolsVersion "25.0.3" buildToolsVersion "26.0.1"

defaultConfig {
minSdkVersion 16
targetSdkVersion 26
}
} }

dependencies {
compile 'com.android.support:support-compat:26.0.0'
}
24 changes: 14 additions & 10 deletions Contacts/Spinners/app/src/main/AndroidManifest.xml
@@ -1,17 +1,21 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.commonsware.android.contacts.spinners"> <manifest package="com.commonsware.android.contacts.spinners"
xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0">


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


<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="18"/> <application

android:icon="@drawable/ic_launcher"
<supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="false"/> android:label="@string/app_name"

android:theme="@style/Theme.Apptheme">
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name"> <activity
<activity android:label="@string/app_name" android:name=".ContactSpinners"> android:name=".ContactSpinners"
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 comments on commit 5d3838a

Please sign in to comment.