Skip to content

Commit

Permalink
[TIMOB-20251]: Enabling request for external storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
hieupham007 committed Feb 3, 2016
1 parent da7a539 commit 85e315f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
Expand Up @@ -8,11 +8,16 @@

import java.util.List;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import org.appcelerator.kroll.KrollFunction;
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.KrollRuntime;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiBaseActivity;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.proxy.IntentProxy;
Expand Down Expand Up @@ -346,6 +351,35 @@ public void stopService(IntentProxy intentProxy)
}
}

@Kroll.method
private boolean hasStoragePermission() {
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) {
return true;
}
return false;
}

@Kroll.method
public void requestStoragePermissions(@Kroll.argument(optional=true)KrollFunction permissionCallback)
{
if (hasStoragePermission()) {
return;
}

if (TiBaseActivity.storageCallbackContext == null) {
TiBaseActivity.storageCallbackContext = getKrollObject();
}
TiBaseActivity.storagePermissionCallback = permissionCallback;
String[] permissions = new String[] {Manifest.permission.READ_EXTERNAL_STORAGE};
Activity currentActivity = TiApplication.getInstance().getCurrentActivity();
currentActivity.requestPermissions(permissions, TiC.PERMISSION_CODE_EXTERNAL_STORAGE);

}

@Kroll.method
public boolean isServiceRunning(IntentProxy intentProxy)
{
Expand Down
Expand Up @@ -93,8 +93,8 @@ public abstract class TiBaseActivity extends AppCompatActivity
private TiWeakList<OnPrepareOptionsMenuEvent> onPrepareOptionsMenuListeners = new TiWeakList<OnPrepareOptionsMenuEvent>();
private APSAnalytics analytics = APSAnalytics.getInstance();

public static KrollObject cameraCallbackContext, contactsCallbackContext, oldCalendarCallbackContext, calendarCallbackContext, locationCallbackContext;
public static KrollFunction cameraPermissionCallback, contactsPermissionCallback, oldCalendarPermissionCallback, calendarPermissionCallback, locationPermissionCallback;
public static KrollObject storageCallbackContext, cameraCallbackContext, contactsCallbackContext, oldCalendarCallbackContext, calendarCallbackContext, locationCallbackContext;
public static KrollFunction storagePermissionCallback, cameraPermissionCallback, contactsPermissionCallback, oldCalendarPermissionCallback, calendarPermissionCallback, locationPermissionCallback;

protected View layout;
protected TiActivitySupportHelper supportHelper;
Expand Down Expand Up @@ -467,6 +467,10 @@ public void onRequestPermissionsResult(int requestCode,
permissionCallback(grantResults, contactsPermissionCallback, contactsCallbackContext, "Contacts");
return;
}
case TiC.PERMISSION_CODE_EXTERNAL_STORAGE: {
permissionCallback(grantResults, storagePermissionCallback, storageCallbackContext, "Storage");
return;
}

}
}
Expand Down
Expand Up @@ -274,6 +274,12 @@ public double spaceAvailable()
public boolean write(Object[] args)
{
try {

if (!TiFileHelper2.hasStoragePermission()) {
Log.e(TAG, "External storage permissions missing");
return false;
}

if (args != null && args.length > 0) {
boolean append = false;
if (args.length > 1 && args[1] instanceof Boolean) {
Expand Down
Expand Up @@ -6,6 +6,10 @@
*/
package org.appcelerator.titanium.util;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;

public class TiFileHelper2
Expand Down Expand Up @@ -77,4 +81,15 @@ public static String getResourceRelativePath(String url)
return null;
}

public static boolean hasStoragePermission() {
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) {
return true;
}
return false;
}

}
22 changes: 22 additions & 0 deletions apidoc/Titanium/Android/Android.yml
Expand Up @@ -192,6 +192,24 @@ methods:
which specifies the service to check.
type: Titanium.Android.Intent

- name: hasStoragePermission
summary: Returns `true` if the app has storage permissions.
returns:
type: Boolean
since: "6.0.0"

- name: requestStoragePermissions
summary: Requests for storage permissions
description: |
On Android, the request view will show if the permission is not accepted by the user, and the user did
not check the box "Never ask again" when denying the request. If the user checks the box "Never ask again,"
the user has to manually enable the permission in device settings.
parameters:
- name: callback
summary: Function to call upon user decision to grant storage access
type: Callback<RequestStorageAccessResult>
since: "6.0.0"

- name: registerBroadcastReceiver
summary: Registers broadcast receiver for the given actions
parameters:
Expand Down Expand Up @@ -2195,3 +2213,7 @@ properties:
optional: true
default: Titanium.Android.START_REDELIVER_INTENT
constants: Titanium.Android.START_*
---
name: RequestStorageAccessResult
summary: Argument passed to the callback when a request finishes successfully or erroneously.
extends: ErrorResponse

0 comments on commit 85e315f

Please sign in to comment.