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
e7fd0ad
commit 5d3838a
Showing
24 changed files
with
364 additions
and
44 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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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' | |||
} |
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
105 changes: 105 additions & 0 deletions
105
...pp/src/main/java/com/commonsware/android/calllog/consumer/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,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()])); | |||
} | |||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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> |
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,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> |
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,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> |
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,6 @@ | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<resources> | |||
|
|||
<style name="Theme.Apptheme" parent="@android:style/Theme.Holo.Light.DarkActionBar"> | |||
</style> | |||
</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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 | |||
} | |||
} | } |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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> |
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,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> |
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,6 @@ | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<resources> | |||
|
|||
<style name="Theme.Apptheme" parent="@android:style/Theme.Holo.Light.DarkActionBar"> | |||
</style> | |||
</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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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' | |||
} |
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
Oops, something went wrong.