android.support.v4.content.PermissionChecker Java Examples

The following examples show how to use android.support.v4.content.PermissionChecker. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocationServiceImpl.java    From Cangol-appcore with Apache License 2.0 6 votes vote down vote up
private boolean checkLocationPermission(Activity activity) {
    List<String> list = new ArrayList<>();
    if (PermissionChecker.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PermissionChecker.PERMISSION_GRANTED) {
        list.add(Manifest.permission.ACCESS_FINE_LOCATION);
    } else {
        Log.e(TAG, "requestLocation need Permission " + Manifest.permission.ACCESS_FINE_LOCATION);
    }
    if (PermissionChecker.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PermissionChecker.PERMISSION_GRANTED) {
        list.add(Manifest.permission.ACCESS_COARSE_LOCATION);
    } else {
        Log.e(TAG, "requestLocation  NETWORK_PROVIDER is disabled ");
    }
    if (!list.isEmpty()) {
        Log.e(TAG, "requestLocation need Permission " + list.toString());
        String[] permissions = new String[list.size()];
        list.toArray(permissions);
        mBetterLocationListener.needPermission(permissions);
        return false;
    }
    return true;
}
 
Example #2
Source File: PermissMeUtilsTest.java    From PermissMe with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDeniedPermissions_whenMixPermissionsGrantedAndUngranted_returnUngrantedPermissions() {
	// Case 3: 2 permissions, 1 ungranted, 1 granted
	BDDMockito.given(
			PermissionChecker.checkSelfPermission(
					any(Context.class),
					eq(Manifest.permission.WRITE_EXTERNAL_STORAGE)
			)
	).willReturn(PackageManager.PERMISSION_DENIED);

	BDDMockito.given(
			PermissionChecker.checkSelfPermission(any(Context.class), eq(Manifest.permission.READ_SMS))
	).willReturn(PackageManager.PERMISSION_GRANTED);

	final String[] deniedPermissions = PermissMeUtils.getDeniedPermissions(
			mockContext,
			Manifest.permission.WRITE_EXTERNAL_STORAGE,
			Manifest.permission.READ_SMS
	);
	assertNotNull(deniedPermissions);
	assertTrue(deniedPermissions.length == 1);
	assertTrue(deniedPermissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE));
}
 
Example #3
Source File: PermissMeUtilsTest.java    From PermissMe with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDeniedPermissions_whenAllPermissionsUngranted_returnAllPermissions() {
	// Case 4: 2 permissions, 2 ungranted
	BDDMockito.given(
			PermissionChecker.checkSelfPermission(any(Context.class), anyString())
	).willReturn(PackageManager.PERMISSION_DENIED);

	String[] deniedPermissions = PermissMeUtils.getDeniedPermissions(
			mockContext,
			Manifest.permission.WRITE_EXTERNAL_STORAGE,
			Manifest.permission.READ_SMS
	);
	assertNotNull(deniedPermissions);
	assertTrue(deniedPermissions.length == 2);
	assertTrue(deniedPermissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE));
	assertTrue(deniedPermissions[1].equals(Manifest.permission.READ_SMS));
}
 
Example #4
Source File: AcpService.java    From SprintNBA with Apache License 2.0 6 votes vote down vote up
/**
 * 检查权限授权状态
 *
 * @param context
 * @param permission
 * @return
 */
int checkSelfPermission(Context context, String permission) {
    try {
        final PackageInfo info = context.getPackageManager().getPackageInfo(
                context.getPackageName(), 0);
        int targetSdkVersion = info.applicationInfo.targetSdkVersion;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (targetSdkVersion >= Build.VERSION_CODES.M) {
                Log.i(TAG, "targetSdkVersion >= Build.VERSION_CODES.M");
                return ContextCompat.checkSelfPermission(context, permission);
            } else {
                return PermissionChecker.checkSelfPermission(context, permission);
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return ContextCompat.checkSelfPermission(context, permission);
}
 
Example #5
Source File: LocationFragment.java    From android with Apache License 2.0 6 votes vote down vote up
protected boolean requestLocationPermissions() {
    int permission = PermissionChecker.checkSelfPermission(getActivity(),
            Manifest.permission.ACCESS_FINE_LOCATION);

    if (permission == PermissionChecker.PERMISSION_GRANTED) {
        return true;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        requestPermissions(new String[] {
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION
        }, DEFAULT_PERMISSIONS_REQUEST_CODE);

        return false;
    }

    return true;
}
 
Example #6
Source File: PermissionHandler.java    From Auth0.Android with MIT License 6 votes vote down vote up
/**
 * Called when there is a new response for a Android Manifest Permission request
 *
 * @param requestCode  received.
 * @param permissions  the Android Manifest Permissions that were requested
 * @param grantResults the grant result for each permission
 * @return the list of Android Manifest Permissions that were declined by the user.
 */
public List<String> parseRequestResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode != this.lastRequestCode) {
        Log.d(TAG, String.format("The received Request Code doesn't match the expected one. Was %d but expected %d", requestCode, this.lastRequestCode));
        return Arrays.asList(permissions);
    } else if (permissions.length == 0 && grantResults.length == 0) {
        Log.w(TAG, "All the required permissions were declined by the user.");
        return Arrays.asList(permissions);
    }

    List<String> declinedPermissions = new ArrayList<>();
    for (int i = 0; i < permissions.length; i++) {
        if (grantResults[i] != PermissionChecker.PERMISSION_GRANTED) {
            declinedPermissions.add(permissions[i]);
        }
    }
    if (!declinedPermissions.isEmpty()) {
        Log.w(TAG, String.format("%d permissions were explicitly declined by the user.", declinedPermissions.size()));
    }
    return declinedPermissions;
}
 
Example #7
Source File: CaptureActivity.java    From ZbarCode with Apache License 2.0 6 votes vote down vote up
private void checkPermissionCamera() {
    int checkPermission = 0;
    if (Build.VERSION.SDK_INT >= 23) {
        // checkPermission =ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA);
        checkPermission = PermissionChecker.checkSelfPermission(this, Manifest.permission.CAMERA);
        if (checkPermission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CAMERA},
                    MY_PERMISSIONS_REQUEST_CAMERA);
        } else {
            isOpenCamera = true;
        }

    } else {
        checkPermission = checkPermission(26);
        if (checkPermission == AppOpsManager.MODE_ALLOWED) {
            isOpenCamera = true;
        } else if (checkPermission == AppOpsManager.MODE_IGNORED) {
            isOpenCamera = false;
            displayFrameworkBugMessageAndExit();
        }
    }
}
 
Example #8
Source File: PairingActivity.java    From microbit with Apache License 2.0 6 votes vote down vote up
@Override
public void checkTelephonyPermissions() {
    if(!requestPermissions.isEmpty()) {
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PermissionChecker.PERMISSION_GRANTED ||
                (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PermissionChecker.PERMISSION_GRANTED)) {
            requestingPermission = requestPermissions.get(0);
            requestPermissions.remove(0);
            PopUp.show((requestingPermission == EventCategories.IPC_BLE_NOTIFICATION_INCOMING_CALL) ? getString(R.string
                            .telephony_permission) : getString(R.string.sms_permission),
                    getString(R.string.permissions_needed_title),
                    R.drawable.message_face, R.drawable.blue_btn, PopUp.GIFF_ANIMATION_NONE,
                    PopUp.TYPE_CHOICE,
                    notificationOKHandler,
                    notificationCancelHandler);
        }
    }
}
 
Example #9
Source File: ProjectActivity.java    From microbit with Apache License 2.0 6 votes vote down vote up
@Override
public void checkTelephonyPermissions() {
    if(!mRequestPermissions.isEmpty()) {
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS)
                != PermissionChecker.PERMISSION_GRANTED ||
                (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
                        != PermissionChecker.PERMISSION_GRANTED)) {
            mRequestingPermission = mRequestPermissions.get(0);
            mRequestPermissions.remove(0);
            PopUp.show((mRequestingPermission == EventCategories.IPC_BLE_NOTIFICATION_INCOMING_CALL)
                            ? getString(R.string.telephony_permission)
                            : getString(R.string.sms_permission),
                    getString(R.string.permissions_needed_title),
                    R.drawable.message_face, R.drawable.blue_btn, PopUp.GIFF_ANIMATION_NONE,
                    PopUp.TYPE_CHOICE,
                    notificationOKHandler,
                    notificationCancelHandler);
        }
    }
}
 
Example #10
Source File: BaseActivity.java    From photo-editor-android with MIT License 6 votes vote down vote up
protected void startCameraActivity() {
    int permissionCheck = PermissionChecker.checkCallingOrSelfPermission(this,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
        Intent photoPickerIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                getOutputMediaFile());
        photoPickerIntent.putExtra("outputFormat",
                Bitmap.CompressFormat.JPEG.toString());
        startActivityForResult(
                Intent.createChooser(photoPickerIntent, getString(R.string.upload_picker_title)),
                CAMERA_CODE);
    } else {
        showMenu(1);
    }
}
 
Example #11
Source File: EmuCheckUtil.java    From CacheEmulatorChecker with Apache License 2.0 6 votes vote down vote up
public static boolean checkPermissionGranted(Context context, String permission) {

        boolean result = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            try {
                final PackageInfo info = context.getPackageManager().getPackageInfo(
                        context.getPackageName(), 0);
                int targetSdkVersion = info.applicationInfo.targetSdkVersion;
                if (targetSdkVersion >= Build.VERSION_CODES.M) {
                    result = context.checkSelfPermission(permission)
                            == PackageManager.PERMISSION_GRANTED;
                } else {
                    result = PermissionChecker.checkSelfPermission(context, permission)
                            == PermissionChecker.PERMISSION_GRANTED;
                }
            } catch (Exception e) {
            }
        }
        return result;
    }
 
Example #12
Source File: CaptureActivity.java    From ScanZbar with Apache License 2.0 6 votes vote down vote up
private void checkPermissionCamera() {
    int checkPermission = 0;
    if (Build.VERSION.SDK_INT >= 23) {
        // checkPermission =ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA);
        checkPermission = PermissionChecker.checkSelfPermission(this, Manifest.permission.CAMERA);
        if (checkPermission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CAMERA},
                    MY_PERMISSIONS_REQUEST_CAMERA);
        } else {
            isOpenCamera = true;
        }

    } else {
        checkPermission = checkPermission(26);
        if (checkPermission == AppOpsManager.MODE_ALLOWED) {
            isOpenCamera = true;
        } else if (checkPermission == AppOpsManager.MODE_IGNORED) {
            isOpenCamera = false;
            displayFrameworkBugMessageAndExit();
        }
    }
}
 
Example #13
Source File: CustomLocationTrackingActivity.java    From android-map-sdk with Apache License 2.0 6 votes vote down vote up
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
    @NonNull int[] grantResults) {

    if (requestCode == PERMISSION_REQUEST_CODE) {
        for (int grantResult : grantResults) {
            if (grantResult != PermissionChecker.PERMISSION_GRANTED) {
                fab.setImageResource(R.drawable.ic_my_location_black_24dp);
                return;
            }
        }

        enableLocation();
        return;
    }

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
 
Example #14
Source File: Activity_Main.java    From Pedometer with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(final Bundle b) {
    super.onCreate(b);
    startService(new Intent(this, SensorListener.class));
    if (b == null) {
        // Create new fragment and transaction
        Fragment newFragment = new Fragment_Overview();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this
        // fragment,
        // and add the transaction to the back stack
        transaction.replace(android.R.id.content, newFragment);

        // Commit the transaction
        transaction.commit();
    }

    if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= 23 && PermissionChecker
            .checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
            PermissionChecker.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
    }
}
 
Example #15
Source File: Main.java    From PictureChooser with Apache License 2.0 6 votes vote down vote up
private void checkPermission(boolean askForPermission) {
    if (Build.VERSION.SDK_INT >= 23 && PermissionChecker
            .checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) !=
            PermissionChecker.PERMISSION_GRANTED && PermissionChecker
            .checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
            PermissionChecker.PERMISSION_GRANTED) {
        if (askForPermission) {
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE_PERMISSION);
        } else {
            finish();
        }
    } else {
        start();
    }
}
 
Example #16
Source File: PeriodicEpgSyncJobServiceTest.java    From androidtv-sample-inputs with Apache License 2.0 6 votes vote down vote up
@Test
public void testPeriodicSync() throws InterruptedException {
    // Do many syncs in a small period of time and make sure they all start
    // Tests that a sync can be requested
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
            mSyncStatusChangedReceiver,
            new IntentFilter(EpgSyncJobService.ACTION_SYNC_STATUS_CHANGED));
    // The CountDownLatch decrements every time sync starts
    mSyncStatusLatch = new CountDownLatch(NUMBER_OF_SYNCS);
    EpgSyncJobService.cancelAllSyncRequests(getActivity());

    // Make sure that we can set up a sync that can persist after boot
    assertEquals(getActivity().checkSelfPermission(Manifest.permission.RECEIVE_BOOT_COMPLETED),
            PermissionChecker.PERMISSION_GRANTED);
    EpgSyncJobService.setUpPeriodicSync(getActivity(), mInputId,
            new ComponentName(getActivity(), TestJobService.class),
            SYNC_PERIOD, SYNC_DURATION); // 15m is the lowest period
    // Wait for every sync to start, with some leeway.
    long timeoutSeconds = SYNC_PERIOD * (NUMBER_OF_SYNCS + 1);
    boolean syncStatusLatchComplete = mSyncStatusLatch.await(timeoutSeconds,
            TimeUnit.MILLISECONDS);
    if (!syncStatusLatchComplete) {
        Assert.fail("Syncing did not complete. The remaining count is " +
                mSyncStatusLatch.getCount() + " after " + timeoutSeconds + " seconds.");
    }
}
 
Example #17
Source File: PermissionManager.java    From Musicoco with Apache License 2.0 6 votes vote down vote up
public boolean checkPermission(Context context, String... permission) {

        boolean nr = true;

        for (int i = 0; i < permission.length; i++) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // targetSdkVersion >= Android M, we can
                // use Context#checkSelfPermission
                nr = context.checkSelfPermission(permission[i])
                        == PackageManager.PERMISSION_GRANTED;
            } else {
                // targetSdkVersion < Android M, we have to use PermissionChecker
                nr = PermissionChecker.checkSelfPermission(context, permission[i])
                        == PermissionChecker.PERMISSION_GRANTED;
            }

            if (!nr) {
                break;
            }
        }
        return nr;
    }
 
Example #18
Source File: SampleAct.java    From RxDownloader with MIT License 5 votes vote down vote up
private void checkPermission() {
    final String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
    int permissionCheck = ContextCompat.checkSelfPermission(this, permission);

    if (permissionCheck != PermissionChecker.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{permission}, 0);
        return;
    }
    startSample();
}
 
Example #19
Source File: Map.java    From MapsMeasure with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
@Override
public void onCreate(final Bundle savedInstanceState) {
    if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= 23 && PermissionChecker
            .checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
            PermissionChecker.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
    }
    try {
        super.onCreate(savedInstanceState);
    } catch (final BadParcelableException bpe) {
        if (BuildConfig.DEBUG) Logger.log(bpe);
    }
    init();
}
 
Example #20
Source File: BaseActivity.java    From Album with Apache License 2.0 5 votes vote down vote up
private static List<String> getDeniedPermissions(Context context, String... permissions) {
    List<String> deniedList = new ArrayList<>(2);
    for (String permission : permissions) {
        if (PermissionChecker.checkSelfPermission(context, permission) != PermissionChecker.PERMISSION_GRANTED) {
            deniedList.add(permission);
        }
    }
    return deniedList;
}
 
Example #21
Source File: Map.java    From MapsMeasure with Apache License 2.0 5 votes vote down vote up
@SuppressLint("MissingPermission")
@Override
public void onRequestPermissionsResult(int requestCode, final String[] permissions,
                                       final int[] grantResults) {
    switch (requestCode) {
        case REQUEST_LOCATION_PERMISSION:
            if (grantResults.length > 0 &&
                    grantResults[0] == PermissionChecker.PERMISSION_GRANTED) {
                getCurrentLocation(lastLocationCallback);
                mMap.setMyLocationEnabled(true);
            } else {
                String savedLocation = getSharedPreferences("settings", Context.MODE_PRIVATE)
                        .getString("lastLocation", null);
                if (savedLocation != null && savedLocation.contains("#")) {
                    String[] data = savedLocation.split("#");
                    try {
                        if (data.length == 3 && mMap != null) {
                            moveCamera(new LatLng(Double.parseDouble(data[0]),
                                    Double.parseDouble(data[1])), Float.parseFloat(data[2]));
                        }
                    } catch (NumberFormatException nfe) {
                        nfe.printStackTrace();
                    }
                }
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
 
Example #22
Source File: PermissMeUtilsTest.java    From PermissMe with Apache License 2.0 5 votes vote down vote up
@Test
public void testNeedToRequestPermission_whenDeviceMarshmallowAndGrantedPermissions_returnFalse() throws Exception {
	setEnvBuildVersion(Build.VERSION_CODES.M);
	final String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;

	// Case 2: Granted permission
	BDDMockito.given(
			PermissionChecker.checkSelfPermission(any(Context.class), eq(permission))
	).willReturn(PackageManager.PERMISSION_GRANTED);
	assertFalse(PermissMeUtils.needToRequestPermission(mockContext, permission));
}
 
Example #23
Source File: Map.java    From MapsMeasure with Apache License 2.0 5 votes vote down vote up
private boolean hasLocationPermission() {
    return PermissionChecker
            .checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) ==
            PermissionChecker.PERMISSION_GRANTED && PermissionChecker
            .checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) ==
            PermissionChecker.PERMISSION_GRANTED;
}
 
Example #24
Source File: PermissMeUtilsTest.java    From PermissMe with Apache License 2.0 5 votes vote down vote up
@Test
public void testNeedToRequestPermission_whenDeviceMarshmallowAndUngrantedPermissions_returnTrue() throws Exception {
	setEnvBuildVersion(Build.VERSION_CODES.M);

	final String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;

	// Case 1: Ungranted permission
	BDDMockito.given(
			PermissionChecker.checkSelfPermission(any(Context.class), eq(permission))
	).willReturn(PackageManager.PERMISSION_DENIED);

	assertTrue(PermissMeUtils.needToRequestPermission(mockContext, permission));
}
 
Example #25
Source File: PermissMeUtilsTest.java    From PermissMe with Apache License 2.0 5 votes vote down vote up
@Test
public void testPermissionIsInvalidOrHasPermission_whenPermissionValidAndGranted_returnTrue() {
	final String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
	// Case 4: Granted permission
	BDDMockito.given(
			PermissionChecker.checkSelfPermission(any(Context.class), eq(permission))
	).willReturn(PackageManager.PERMISSION_GRANTED);
	final boolean result = PermissMeUtils.permissionIsInvalidOrHasPermission(mockContext, permission);
	assertTrue(result);
}
 
Example #26
Source File: PermissMeUtilsTest.java    From PermissMe with Apache License 2.0 5 votes vote down vote up
@Test
public void testPermissionIsInvalidOrHasPermission_whenPermissionValidAndUngranted_returnFalse() {
	final String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
	// Case 3: Ungranted permission
	BDDMockito.given(
			PermissionChecker.checkSelfPermission(any(Context.class), eq(permission))
	).willReturn(PackageManager.PERMISSION_DENIED);
	final boolean result = PermissMeUtils.permissionIsInvalidOrHasPermission(mockContext, permission);
	assertFalse(result);
}
 
Example #27
Source File: Activity_Main.java    From Pedometer with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(final Bundle b) {
    super.onCreate(b);
    if (Build.VERSION.SDK_INT >= 26) {
        API26Wrapper.startForegroundService(this, new Intent(this, SensorListener.class));
    } else {
        startService(new Intent(this, SensorListener.class));
    }
    if (b == null) {
        // Create new fragment and transaction
        Fragment newFragment = new Fragment_Overview();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this
        // fragment,
        // and add the transaction to the back stack
        transaction.replace(android.R.id.content, newFragment);

        // Commit the transaction
        transaction.commit();
    }


    GoogleApiClient.Builder builder = new GoogleApiClient.Builder(this, this, this);
    builder.addApi(Games.API, Games.GamesOptions.builder().build());
    builder.addScope(Games.SCOPE_GAMES);
    builder.addApi(Fitness.HISTORY_API);
    builder.addApi(Fitness.RECORDING_API);
    builder.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE));

    mGoogleApiClient = builder.build();

    if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= 23 && PermissionChecker
            .checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
            PermissionChecker.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
    }
}
 
Example #28
Source File: MainActivity.java    From apkextractor with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode==0){
        if(grantResults.length==0)return;
        if(grantResults[0]==PermissionChecker.PERMISSION_GRANTED){
            sendBroadcast(new Intent(Constants.ACTION_REFRESH_IMPORT_ITEMS_LIST));
        }
    }
}
 
Example #29
Source File: PermissMeUtilsTest.java    From PermissMe with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetDeniedPermissions_whenOnePermissionsUngranted_returnThatOnePermission() {
	// Case 1: Ungranted permissions
	BDDMockito.given(PermissionChecker.checkSelfPermission(any(Context.class), anyString())).willReturn
			(PackageManager.PERMISSION_DENIED);

	final String[] deniedPermissions = PermissMeUtils.getDeniedPermissions(
			mockContext,
			Manifest.permission.WRITE_EXTERNAL_STORAGE
	);
	assertNotNull(deniedPermissions);
	assertTrue(deniedPermissions.length > 0);
	assertTrue(deniedPermissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE));
}
 
Example #30
Source File: PermissionUtil.java    From imsdk-android with MIT License 5 votes vote down vote up
public static boolean checkAndRequestPermissionsInActivity(Activity cxt, String... checkPermissions) {
    boolean isHas = true;
    List<String> permissions = new ArrayList<>();
    for (String checkPermission : checkPermissions) {
        if (PermissionChecker.checkSelfPermission(cxt, checkPermission) != PackageManager.PERMISSION_GRANTED) {
            isHas = false;
            permissions.add(checkPermission);
        }
    }
    if (!isHas) {
        String[] p = permissions.toArray(new String[permissions.size()]);
        requestPermissionsInActivity(cxt, Code.REQUEST_PERMISSION, p);
    }
    return isHas;
}