Skip to content

Commit

Permalink
Merge pull request #8800 from garymathews/TIMOB-24304
Browse files Browse the repository at this point in the history
[6_0_X][TIMOB-24304] Android: Use application context for permissions
  • Loading branch information
mukherjee2 committed Feb 16, 2017
2 parents cda184e + be58569 commit cffce3b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ public boolean hasPermission(String permission) {
if (Build.VERSION.SDK_INT < 23) {
return true;
}
Activity currentActivity = TiApplication.getInstance().getCurrentActivity();
if (currentActivity.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {
Context context = TiApplication.getInstance().getApplicationContext();
if (context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
Expand Down Expand Up @@ -65,10 +66,10 @@ public boolean hasContactsPermissions() {
if (Build.VERSION.SDK_INT < 23) {
return true;
}
Activity currentActivity = TiApplication.getAppCurrentActivity();
Context context = TiApplication.getInstance().getApplicationContext();
// If READ_CONTACTS is granted, WRITE_CONTACTS is also granted if the permission is included in manifest.
if (currentActivity != null &&
currentActivity.checkSelfPermission(Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
if (context != null &&
context.checkSelfPermission(Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
return true;
}
Log.w(TAG, "Contact permissions are missing");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.net.MalformedURLException;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import org.appcelerator.kroll.KrollFunction;
Expand Down Expand Up @@ -89,8 +90,8 @@ private boolean hasStoragePermissions() {
if (Build.VERSION.SDK_INT < 23) {
return true;
}
Activity currentActivity = TiApplication.getInstance().getCurrentActivity();
if (currentActivity.checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Context context = TiApplication.getInstance().getApplicationContext();
if (context.checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import ti.modules.titanium.geolocation.android.LocationRuleProxy;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
Expand Down Expand Up @@ -618,8 +619,8 @@ public boolean hasLocationPermissions()
if (Build.VERSION.SDK_INT < 23) {
return true;
}
Activity currentActivity = TiApplication.getInstance().getCurrentActivity();
if (currentActivity.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Context context = TiApplication.getInstance().getApplicationContext();
if (context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,9 @@ public boolean hasCameraPermissions() {
if (Build.VERSION.SDK_INT < 23) {
return true;
}
Activity currentActivity = TiApplication.getInstance().getCurrentActivity();
if (currentActivity.checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
currentActivity.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Context context = TiApplication.getInstance().getApplicationContext();
if (context.checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
context.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
return true;
}
return false;
Expand All @@ -367,8 +367,8 @@ private boolean hasCameraPermission() {
if (Build.VERSION.SDK_INT < 23) {
return true;
}
Activity currentActivity = TiApplication.getInstance().getCurrentActivity();
if (currentActivity.checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
Context context = TiApplication.getInstance().getApplicationContext();
if (context.checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
return true;
}
return false;
Expand All @@ -378,8 +378,8 @@ private boolean hasStoragePermission() {
if (Build.VERSION.SDK_INT < 23) {
return true;
}
Activity currentActivity = TiApplication.getInstance().getCurrentActivity();
if (currentActivity.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Context context = TiApplication.getInstance().getApplicationContext();
if (context.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.appcelerator.titanium.util;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import org.appcelerator.titanium.TiApplication;
Expand Down Expand Up @@ -85,12 +86,12 @@ public static boolean hasStoragePermission() {
if (Build.VERSION.SDK_INT < 23) {
return true;
}
Activity currentActivity = TiApplication.getInstance().getCurrentActivity();
Context context = TiApplication.getInstance().getApplicationContext();
// Fix for TIMOB-20434 where activity is null
if (currentActivity == null) {
if (context == null) {
return false;
}
if (currentActivity.checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
if (context.checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
return true;
}
return false;
Expand Down

0 comments on commit cffce3b

Please sign in to comment.