Commiting code to handle permissions, and the special case of the Geolocation Plugin

This commit is contained in:
Joe Bowser 2015-07-14 14:31:07 -07:00
parent 7d61a79a78
commit 8981ddb681
6 changed files with 134 additions and 37 deletions

View File

@ -109,24 +109,18 @@ public class CordovaActivity extends Activity {
// need to activate preferences before super.onCreate to avoid "requestFeature() must be called before adding content" exception
loadConfig();
if(!preferences.getBoolean("ShowTitle", false))
{
if (!preferences.getBoolean("ShowTitle", false)) {
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
}
if(preferences.getBoolean("SetFullscreen", false))
{
if (preferences.getBoolean("SetFullscreen", false)) {
Log.d(TAG, "The SetFullscreen configuration is deprecated in favor of Fullscreen, and will be removed in a future version.");
preferences.set("Fullscreen", true);
}
if(preferences.getBoolean("Fullscreen", false))
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
if (preferences.getBoolean("Fullscreen", false)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
immersiveMode = true;
}
else
{
} else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
@ -138,8 +132,7 @@ public class CordovaActivity extends Activity {
super.onCreate(savedInstanceState);
cordovaInterface = makeCordovaInterface();
if(savedInstanceState != null)
{
if (savedInstanceState != null) {
cordovaInterface.restoreInstanceState(savedInstanceState);
}
}
@ -192,7 +185,7 @@ public class CordovaActivity extends Activity {
/**
* Construct the default web view object.
*
* <p/>
* Override this to customize the webview that is used.
*/
protected CordovaWebView makeWebView() {
@ -245,7 +238,7 @@ public class CordovaActivity extends Activity {
/**
* Called when the activity receives a new intent
**/
*/
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
@ -468,8 +461,7 @@ public class CordovaActivity extends Activity {
return null;
}
protected void onSaveInstanceState(Bundle outState)
{
protected void onSaveInstanceState(Bundle outState) {
cordovaInterface.onSaveInstanceState(outState);
super.onSaveInstanceState(outState);
}
@ -490,4 +482,19 @@ public class CordovaActivity extends Activity {
pm.onConfigurationChanged(newConfig);
}
}
/**
* Called by the system when the user grants permissions
*
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[],
int[] grantResults) {
PluginManager pm = this.appView.getPluginManager();
pm.onRequestPermissionResult(requestCode, permissions, grantResults);
}
}

View File

@ -69,4 +69,6 @@ public interface CordovaInterface {
* Returns a shared thread pool that can be used for background tasks.
*/
public ExecutorService getThreadPool();
public void requestPermission(CordovaPlugin plugin);
}

View File

@ -161,4 +161,10 @@ public class CordovaInterfaceImpl implements CordovaInterface {
this.intent = intent;
}
}
public void requestPermission(CordovaPlugin plugin)
{
pluginManager.requestPermission(plugin);
}
}

View File

@ -26,6 +26,7 @@ import org.json.JSONArray;
import org.json.JSONException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.Uri;
@ -40,6 +41,7 @@ public class CordovaPlugin {
public CordovaInterface cordova;
protected CordovaPreferences preferences;
private String serviceName;
private String [] permissions;
/**
* Call this after constructing to initialize the plugin.
@ -359,4 +361,50 @@ public class CordovaPlugin {
*/
public void onConfigurationChanged(Configuration newConfig) {
}
/**
* Called by the Plugin Manager when we need to actually request permissions
*
* @return Returns the permission that was stored in the plugin
*/
public String[] getPermissionRequest() {
return permissions;
}
/**
* requestPermission
*/
public void requestPermission() {
cordova.requestPermission(this);
}
public boolean hasPermisssion() {
for(String p : permissions)
{
if(PackageManager.PERMISSION_DENIED == cordova.getActivity().checkSelfPermission(p))
{
return false;
}
}
}
/**
* Called by the system when the user grants permissions
*
* @param requestCode
* @param permissions
* @param grantResults
*/
public boolean onRequestPermissionResult(int requestCode, String[] permissions,
int[] grantResults) {
for(int r:grantResults)
{
if(r == PackageManager.PERMISSION_DENIED)
return false;
}
return true;
}
}

View File

@ -47,6 +47,8 @@ public class PluginManager {
private final CordovaWebView app;
private boolean isInitialized;
private CordovaPlugin permissionRequester;
public PluginManager(CordovaWebView cordovaWebView, CordovaInterface cordova, Collection<PluginEntry> pluginEntries) {
this.ctx = cordova;
this.app = cordovaWebView;
@ -508,4 +510,27 @@ public class PluginManager {
}
}
}
/**
* Called by the system when the user grants permissions
*
* @param requestCode
* @param permissions
* @param grantResults
*/
public void onRequestPermissionResult(int requestCode, String[] permissions,
int[] grantResults) {
if(permissionRequester != null)
{
permissionRequester.onRequestPermissionResult(requestCode, permissions, grantResults);
permissionRequester = null;
}
}
public void requestPermission(CordovaPlugin plugin) {
permissionRequester = plugin;
String[] permissions = plugin.getPermissionRequest();
int requestCode = 1;
ctx.getActivity().requestPermissions(permissions, requestCode);
}
}

View File

@ -174,12 +174,21 @@ public class SystemWebChromeClient extends WebChromeClient {
/**
* Instructs the client to show a prompt to ask the user to set the Geolocation permission state for the specified origin.
*
* This also checks for the Geolocation Plugin and requests permission from the application to use Geolocation.
*
* @param origin
* @param callback
*/
public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) {
super.onGeolocationPermissionsShowPrompt(origin, callback);
callback.invoke(origin, true, false);
//Get the plugin, it should be loaded
CordovaPlugin geolocation = parentEngine.pluginManager.getPlugin("org.apache.cordova.geolocation.PermissionHandler");
if(geolocation != null && !geolocation.hasPermisssion())
{
parentEngine.pluginManager.requestPermission(geolocation);
}
}
// API level 7 is required for this, see if we could lower this using something else