Example usage for android.app Activity requestPermissions

List of usage examples for android.app Activity requestPermissions

Introduction

In this page you can find the example usage for android.app Activity requestPermissions.

Prototype

public final void requestPermissions(@NonNull String[] permissions, int requestCode) 

Source Link

Document

Requests permissions to be granted to this application.

Usage

From source file:org.artoolkit.ar.samples.ARNativeOSG.CameraSurface.java

@SuppressWarnings("deprecation")
public CameraSurface(Context context) {
    super(context);

    Log.i(TAG, "CameraSurface(): ctor called");
    Activity activityRef = (Activity) context;

    try {/*from www . j  a  v a 2 s. co  m*/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(activityRef,
                    Manifest.permission.CAMERA)) {
                mustAskPermissionFirst = true;
                if (activityRef.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                    // Will drop in here if user denied permissions access camera before.
                    // Or no uses-permission CAMERA element is in the
                    // manifest file. Must explain to the end user why the app wants
                    // permissions to the camera devices.
                    Toast.makeText(activityRef.getApplicationContext(),
                            "App requires access to camera to be granted", Toast.LENGTH_SHORT).show();
                }
                // Request permission from the user to access the camera.
                Log.i(TAG, "CameraSurface(): must ask user for camera access permission");
                activityRef.requestPermissions(new String[] { Manifest.permission.CAMERA },
                        ARNativeOSGActivity.REQUEST_CAMERA_PERMISSION_RESULT);
                return;
            }
        }
    } catch (Exception ex) {
        Log.e(TAG, "CameraSurface(): exception caught, " + ex.getMessage());
        return;
    }

    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // Deprecated in API level 11. Still required for API levels <= 10.
}

From source file:org.artoolkit.ar.samples.ARNative.CameraSurface.java

@SuppressWarnings("deprecation")
public CameraSurface(Context context) {
    super(context);

    Log.i(TAG, "CameraSurface(): ctor called");
    Activity activityRef = (Activity) context;

    try {/*from w w  w.  ja  v a 2 s  .  c  o m*/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(activityRef,
                    Manifest.permission.CAMERA)) {
                mustAskPermissionFirst = true;
                if (activityRef.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                    // Will drop in here if user denied permissions access camera before.
                    // Or no uses-permission CAMERA element is in the
                    // manifest file. Must explain to the end user why the app wants
                    // permissions to the camera devices.
                    Toast.makeText(activityRef.getApplicationContext(),
                            "App requires access to camera to be granted", Toast.LENGTH_SHORT).show();
                }
                // Request permission from the user to access the camera.
                Log.i(TAG, "CameraSurface(): must ask user for camera access permission");
                activityRef.requestPermissions(new String[] { Manifest.permission.CAMERA },
                        ARNativeActivity.REQUEST_CAMERA_PERMISSION_RESULT);
                return;
            }
        }
    } catch (Exception ex) {
        Log.e(TAG, "CameraSurface(): exception caught, " + ex.getMessage());
        return;
    }

    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // Deprecated in API level 11. Still required for API levels <= 10.
}

From source file:com.mobiuso.argo.ARModule.CameraSurface.java

@SuppressWarnings("deprecation")
public CameraSurface(Context context) {
    super(context);

    Log.i(TAG, "CameraSurface(): ctor called");
    Activity activityRef = (Activity) context;

    try {/*ww  w.ja  v  a  2s  . c om*/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(activityRef,
                    Manifest.permission.CAMERA)) {
                mustAskPermissionFirst = true;
                if (activityRef.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                    // Will drop in here if user denied permissions access camera before.
                    // Or no uses-permission CAMERA element is in the
                    // manifest file. Must explain to the end user why the app wants
                    // permissions to the camera devices.
                    Toast.makeText(activityRef.getApplicationContext(),
                            "App requires access to camera to be granted", Toast.LENGTH_SHORT).show();
                }
                // Request permission from the user to access the camera.
                Log.i(TAG, "CameraSurface(): must ask user for camera access permission");
                activityRef.requestPermissions(new String[] { Manifest.permission.CAMERA },
                        ARMovieActivity.REQUEST_CAMERA_PERMISSION_RESULT);
                return;
            }
        }
    } catch (Exception ex) {
        Log.e(TAG, "CameraSurface(): exception caught, " + ex.getMessage());
        return;
    }

    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // Deprecated in API level 11. Still required for API levels <= 10.
}

From source file:ti.modules.titanium.android.AndroidModule.java

@Kroll.method
public void requestPermissions(String[] permissions,
        @Kroll.argument(optional = true) KrollFunction permissionCallback) {
    if (Build.VERSION.SDK_INT < 23) {
        return;/*w  w w . j  av  a2s. c o  m*/
    }
    Activity currentActivity = TiApplication.getInstance().getCurrentActivity();
    ArrayList<String> filteredPermissions = new ArrayList<String>();
    //filter out granted permissions
    for (int i = 0; i < permissions.length; ++i) {
        String perm = permissions[i];
        if (currentActivity.checkSelfPermission(perm) == PackageManager.PERMISSION_GRANTED) {
            continue;
        }
        filteredPermissions.add(perm);
    }

    if (filteredPermissions.size() == 0) {
        Log.w(TAG, "Permission(s) already granted");
        return;
    }
    TiBaseActivity.registerPermissionRequestCallback(REQUEST_CODE, permissionCallback, getKrollObject());
    currentActivity.requestPermissions(filteredPermissions.toArray(new String[filteredPermissions.size()]),
            REQUEST_CODE);
}

From source file:org.artoolkit.ar.samples.nftSimple.CameraSurface.java

@SuppressWarnings("deprecation")
public CameraSurface(Context context) {
    super(context);
    Log.i(TAG, "CameraSurface(): ctor called");
    Activity activityRef = (Activity) context;

    try {//from  w  w  w . j  a v  a 2 s  . co  m
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(activityRef,
                    Manifest.permission.CAMERA)) {
                mustAskPermissionFirst = true;
                if (activityRef.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                    // Will drop in here if user denied permissions access camera before.
                    // Or no uses-permission CAMERA element is in the
                    // manifest file. Must explain to the end user why the app wants
                    // permissions to the camera devices.
                    Toast.makeText(activityRef.getApplicationContext(),
                            "App requires access to camera to be granted", Toast.LENGTH_SHORT).show();
                }
                // Request permission from the user to access the camera.
                Log.i(TAG, "CameraSurface(): must ask user for camera access permission");
                activityRef.requestPermissions(new String[] { Manifest.permission.CAMERA },
                        nftSimpleActivity.REQUEST_CAMERA_PERMISSION_RESULT);
                return;
            }
        }
    } catch (Exception ex) {
        Log.e(TAG, "CameraSurface(): exception caught, " + ex.getMessage());
        return;
    }

    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // Deprecated in API level 11. Still required for API levels <= 10.
}

From source file:org.artoolkit.ar.utils.calib_optical.CameraSurface.java

@SuppressWarnings("deprecation")
public CameraSurface(Context context) {
    super(context);

    Log.i(TAG, "CameraSurface(): ctor called");
    Activity activityRef = (Activity) context;

    try {/*from w w  w  .  j a v  a  2s. c o m*/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(activityRef,
                    Manifest.permission.CAMERA)) {
                mustAskPermissionFirst = true;
                if (activityRef.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                    // Will drop in here if user denied permissions access camera before.
                    // Or no uses-permission CAMERA element is in the
                    // manifest file. Must explain to the end user why the app wants
                    // permissions to the camera devices.
                    Toast.makeText(activityRef.getApplicationContext(),
                            "App requires access to camera to be granted", Toast.LENGTH_SHORT).show();
                }
                // Request permission from the user to access the camera.
                Log.i(TAG, "CameraSurface(): must ask user for camera access permission");
                activityRef.requestPermissions(new String[] { Manifest.permission.CAMERA },
                        calib_optical_Activity.REQUEST_CAMERA_PERMISSION_RESULT);
                return;
            }
        }
    } catch (Exception ex) {
        Log.e(TAG, "CameraSurface(): exception caught, " + ex.getMessage());
        return;
    }

    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // Deprecated in API level 11. Still required for API levels <= 10.
}

From source file:org.artoolkit.ar.base.camera.CaptureCameraPreview.java

/**
 * Constructor takes a {@link CameraEventListener} which will be called on
 * to handle camera related events.//from  w  w w . j a  va  2  s  .  c om
 *
 * @param cel CameraEventListener to use. Can be null.
 */
@SuppressWarnings("deprecation")
public CaptureCameraPreview(Activity activity, CameraEventListener cel) {
    super(activity);

    Log.i(TAG, "CaptureCameraPreview(): ctor called");
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(activity,
                    Manifest.permission.CAMERA)) {
                mustAskPermissionFirst = true;
                if (activity.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                    // Will drop in here if user denied permissions access camera before.
                    // Or no uses-permission CAMERA element is in the
                    // manifest file. Must explain to the end user why the app wants
                    // permissions to the camera devices.
                    Toast.makeText(activity.getApplicationContext(),
                            "App requires access to camera to be granted", Toast.LENGTH_SHORT).show();
                }
                // Request permission from the user to access the camera.
                Log.i(TAG, "CaptureCameraPreview(): must ask user for camera access permission");
                activity.requestPermissions(new String[] { Manifest.permission.CAMERA },
                        REQUEST_CAMERA_PERMISSION_RESULT);
                return;
            }
        }
    } catch (Exception ex) {
        Log.e(TAG, "CaptureCameraPreview(): exception caught, " + ex.getMessage());
        return;
    }

    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // Deprecated in API level 11. Still required for API levels <= 10.

    setCameraEventListener(cel);
}

From source file:dev.nick.accessories.injection.processors.PermissionProcessor.java

@Override
public boolean process(@NonNull Object obj, @NonNull Type type) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
        return false;

    in();/*from   www  .j  a v a2s. c om*/

    Activity activity = null;

    if (obj instanceof Activity) {
        activity = (Activity) obj;
    }

    if (obj instanceof Fragment) {
        Fragment fragment = (Fragment) obj;
        activity = fragment.getActivity();
    }

    if (obj instanceof android.support.v4.app.Fragment) {
        android.support.v4.app.Fragment fragment = (android.support.v4.app.Fragment) obj;
        activity = fragment.getActivity();
    }

    Preconditions.checkNotNull(activity, "Only Activity or Fragment is supported for this annotation.");

    RequestPermissions autoRequirePermission = activity.getClass().getAnnotation(RequestPermissions.class);
    int code = autoRequirePermission.requestCode();

    String[] scope = autoRequirePermission.permissions();
    String[] required;
    if (scope.length == 0) {
        scope = getPkgInfo(activity).requestedPermissions;
        required = extractUnGranted(activity, scope);
    } else {
        required = extractUnGranted(activity, scope);
    }
    if (required == null || required.length == 0) {
        int[] codes = new int[0];
        if (required != null) {
            codes = new int[scope.length];
            for (int i = 0; i < codes.length; i++) {
                codes[i] = PackageManager.PERMISSION_GRANTED;
            }
        }
        activity.onRequestPermissionsResult(code, scope, codes);
    } else {
        activity.requestPermissions(required, code);
    }
    return true;
}

From source file:com.nick.scalpel.core.request.RequestPermissionWirer.java

@Override
public void wire(Object obj) {

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
        return;//from   ww w. j av  a 2 s.c o m

    Activity activity = null;

    if (obj instanceof Activity) {
        activity = (Activity) obj;
    }

    if (obj instanceof Fragment) {
        Fragment fragment = (Fragment) obj;
        activity = fragment.getActivity();
    }

    if (obj instanceof android.support.v4.app.Fragment) {
        android.support.v4.app.Fragment fragment = (android.support.v4.app.Fragment) obj;
        activity = fragment.getActivity();
    }

    Preconditions.checkNotNull(activity, "Only Activity or Fragment(app/v4) is supported for this annotation.");

    RequirePermission autoRequirePermission = activity.getClass().getAnnotation(RequirePermission.class);
    int code = autoRequirePermission.requestCode();

    String[] scope = autoRequirePermission.permissions();
    String[] required;
    if (scope.length == 0) {
        scope = getPkgInfo(activity).requestedPermissions;
        required = extractUnGranted(activity, scope);
    } else {
        required = extractUnGranted(activity, scope);
    }
    if (required == null || required.length == 0) {
        int[] codes = new int[0];
        if (required != null) {
            codes = new int[scope.length];
            for (int i = 0; i < codes.length; i++) {
                codes[i] = PackageManager.PERMISSION_GRANTED;
            }
        }
        activity.onRequestPermissionsResult(code, scope, codes);
    } else {
        activity.requestPermissions(required, code);
    }
}

From source file:org.telegram.ui.ThemeActivity.java

private void updateSunTime(Location lastKnownLocation, boolean forceUpdate) {
    LocationManager locationManager = (LocationManager) ApplicationLoader.applicationContext
            .getSystemService(Context.LOCATION_SERVICE);
    if (Build.VERSION.SDK_INT >= 23) {
        Activity activity = getParentActivity();
        if (activity != null) {
            if (activity.checkSelfPermission(
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                activity.requestPermissions(new String[] { Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION }, 2);
                return;
            }/*w  w w .  j  a  v a  2  s  . c om*/
        }
    }
    if (getParentActivity() != null) {
        if (!getParentActivity().getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS)) {
            return;
        }
        try {
            LocationManager lm = (LocationManager) ApplicationLoader.applicationContext
                    .getSystemService(Context.LOCATION_SERVICE);
            if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
                builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
                builder.setMessage(LocaleController.getString("GpsDisabledAlert", R.string.GpsDisabledAlert));
                builder.setPositiveButton(
                        LocaleController.getString("ConnectingToProxyEnable", R.string.ConnectingToProxyEnable),
                        (dialog, id) -> {
                            if (getParentActivity() == null) {
                                return;
                            }
                            try {
                                getParentActivity().startActivity(
                                        new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                            } catch (Exception ignore) {

                            }
                        });
                builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
                showDialog(builder.create());
                return;
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    }
    try {
        lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (lastKnownLocation == null) {
            lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        } else if (lastKnownLocation == null) {
            lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
    if (lastKnownLocation == null || forceUpdate) {
        startLocationUpdate();
        if (lastKnownLocation == null) {
            return;
        }
    }
    Theme.autoNightLocationLatitude = lastKnownLocation.getLatitude();
    Theme.autoNightLocationLongitude = lastKnownLocation.getLongitude();
    int time[] = SunDate.calculateSunriseSunset(Theme.autoNightLocationLatitude,
            Theme.autoNightLocationLongitude);
    Theme.autoNightSunriseTime = time[0];
    Theme.autoNightSunsetTime = time[1];
    Theme.autoNightCityName = null;
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    Theme.autoNightLastSunCheckDay = calendar.get(Calendar.DAY_OF_MONTH);
    Utilities.globalQueue.postRunnable(() -> {
        String name;
        try {
            Geocoder gcd = new Geocoder(ApplicationLoader.applicationContext, Locale.getDefault());
            List<Address> addresses = gcd.getFromLocation(Theme.autoNightLocationLatitude,
                    Theme.autoNightLocationLongitude, 1);
            if (addresses.size() > 0) {
                name = addresses.get(0).getLocality();
            } else {
                name = null;
            }
        } catch (Exception ignore) {
            name = null;
        }
        final String nameFinal = name;
        AndroidUtilities.runOnUIThread(() -> {
            Theme.autoNightCityName = nameFinal;
            if (Theme.autoNightCityName == null) {
                Theme.autoNightCityName = String.format("(%.06f, %.06f)", Theme.autoNightLocationLatitude,
                        Theme.autoNightLocationLongitude);
            }
            Theme.saveAutoNightThemeConfig();
            if (listView != null) {
                RecyclerListView.Holder holder = (RecyclerListView.Holder) listView
                        .findViewHolderForAdapterPosition(scheduleUpdateLocationRow);
                if (holder != null && holder.itemView instanceof TextSettingsCell) {
                    ((TextSettingsCell) holder.itemView).setTextAndValue(LocaleController
                            .getString("AutoNightUpdateLocation", R.string.AutoNightUpdateLocation),
                            Theme.autoNightCityName, false);
                }
            }
        });
    });
    RecyclerListView.Holder holder = (RecyclerListView.Holder) listView
            .findViewHolderForAdapterPosition(scheduleLocationInfoRow);
    if (holder != null && holder.itemView instanceof TextInfoPrivacyCell) {
        ((TextInfoPrivacyCell) holder.itemView).setText(getLocationSunString());
    }
    if (Theme.autoNightScheduleByLocation && Theme.selectedAutoNightType == Theme.AUTO_NIGHT_TYPE_SCHEDULED) {
        Theme.checkAutoNightThemeConditions();
    }
}