-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from jainsahab/master
runtime permissions for android #36
- Loading branch information
Showing
4 changed files
with
98 additions
and
6 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
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,28 @@ | ||
package nsit.app.com.nsitapp; | ||
|
||
import android.os.Bundle; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
import android.support.v7.app.AppCompatActivity; | ||
|
||
import nsit.app.com.nsitapp.helper.AppPermissionChecker; | ||
|
||
/** | ||
* Created by prateekjain on 10/31/16. | ||
*/ | ||
public class BaseActivity extends AppCompatActivity { | ||
protected AppPermissionChecker appPermissionChecker; | ||
|
||
@Override | ||
protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
appPermissionChecker = new AppPermissionChecker(this); | ||
} | ||
|
||
@Override | ||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | ||
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | ||
appPermissionChecker.handlePermissionResult(requestCode, permissions, grantResults); | ||
} | ||
|
||
} |
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
50 changes: 50 additions & 0 deletions
50
app/src/main/java/nsit/app/com/nsitapp/helper/AppPermissionChecker.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,50 @@ | ||
package nsit.app.com.nsitapp.helper; | ||
|
||
import android.app.Activity; | ||
import android.content.pm.PackageManager; | ||
import android.os.Build; | ||
import android.support.v4.app.ActivityCompat; | ||
import android.support.v4.content.ContextCompat; | ||
|
||
/** | ||
* Created by prateekjain on 10/31/16. | ||
*/ | ||
public class AppPermissionChecker { | ||
private int permissionRequestCode; | ||
private PermissionRequestCallBack callBack; | ||
private Activity activity; | ||
|
||
public AppPermissionChecker(Activity activity) { | ||
this.activity = activity; | ||
} | ||
|
||
public void handlePermission(String permission, int permissionRequestCode, PermissionRequestCallBack callBack) { | ||
this.permissionRequestCode = permissionRequestCode; | ||
this.callBack = callBack; | ||
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) { | ||
ActivityCompat.requestPermissions(activity, new String[]{permission}, permissionRequestCode); | ||
} else { | ||
callBack.permissionGranted(); | ||
} | ||
} else { | ||
callBack.permissionGranted(); | ||
} | ||
} | ||
|
||
public void handlePermissionResult(int requestCode, String[] permissions, int[] grantResults) { | ||
if (requestCode == permissionRequestCode && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | ||
callBack.permissionGranted(); | ||
} else { | ||
callBack.permissionDenied(); | ||
} | ||
|
||
} | ||
|
||
public interface PermissionRequestCallBack { | ||
void permissionGranted(); | ||
|
||
void permissionDenied(); | ||
} | ||
|
||
} |