Java Code Examples for android.support.v4.content.PermissionChecker#PERMISSION_GRANTED

The following examples show how to use android.support.v4.content.PermissionChecker#PERMISSION_GRANTED . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
Source File: PermissMeUtils.java    From PermissMe with Apache License 2.0 5 votes vote down vote up
/**
 * Check that all given permissions have been granted by verifying that each entry in the
 * given array is of the value {@link PermissionChecker#PERMISSION_GRANTED}.
 *
 * @param grantResults
 * 		the results to check whether they are granted
 * @return whether the permissions have been granted
 *
 * @see Activity#onRequestPermissionsResult(int, String[], int[])
 */
public static boolean verifyPermissions(@NonNull final int[] grantResults) {
	// At least one result must be checked.
	if (grantResults.length < 1) {
		return false;
	}

	// Verify that each required permission has been granted, otherwise return false.
	for (int result : grantResults) {
		if (result != PermissionChecker.PERMISSION_GRANTED) {
			return false;
		}
	}
	return true;
}
 
Example 12
Source File: HomeActivity.java    From microbit with Apache License 2.0 5 votes vote down vote up
/**
 * Checks and requests for external storage permissions
 * if the app is started at the first time.
 */
private void checkMinimumPermissionsForThisScreen() {
    //Check reading permissions & writing permission to populate the HEX files & show program list
    if(mPrefs.getBoolean(FIRST_RUN, true)) {
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PermissionChecker.PERMISSION_GRANTED ||
                (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        != PermissionChecker.PERMISSION_GRANTED)) {
            PopUp.show(getString(R.string.storage_permission_for_samples),
                    getString(R.string.permissions_needed_title),
                    R.drawable.message_face, R.drawable.blue_btn, PopUp.GIFF_ANIMATION_NONE,
                    PopUp.TYPE_CHOICE,
                    diskStoragePermissionOKHandler,
                    diskStoragePermissionCancelHandler);
        } else {
            if(mPrefs.getBoolean(FIRST_RUN, true)) {
                mPrefs.edit().putBoolean(FIRST_RUN, false).apply();
                //First Run. Install the Sample applications
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        FileUtils.installSamples();
                    }
                }).start();
            }
        }
    }
}
 
Example 13
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 14
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 15
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 16
Source File: ProjectActivity.java    From microbit with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    MBApp application = MBApp.getApp();

    if(savedInstanceState == null) {
        mActivityState = FlashActivityState.STATE_IDLE;

        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(application);

        IntentFilter broadcastIntentFilter = new IntentFilter(IPCConstants.INTENT_BLE_NOTIFICATION);
        localBroadcastManager.registerReceiver(connectionChangedReceiver, broadcastIntentFilter);

        localBroadcastManager.registerReceiver(gattForceClosedReceiver, new IntentFilter(BLEService
                .GATT_FORCE_CLOSED));
    }

    logi("onCreate() :: ");

    // Make sure to call this before any other userActionEvent is sent
    GoogleAnalyticsManager.getInstance().sendViewEventStats(ProjectActivity.class.getSimpleName());

    //Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_projects);
    initViews();
    setupFontStyle();

    minimumPermissionsGranted = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
            == PermissionChecker.PERMISSION_GRANTED && (ContextCompat.checkSelfPermission(this, Manifest
            .permission.WRITE_EXTERNAL_STORAGE) == PermissionChecker.PERMISSION_GRANTED);

    checkMinimumPermissionsForThisScreen();
    setConnectedDeviceText();

    if(savedInstanceState == null && getIntent() != null) {
        handleIncomingIntent(getIntent());
    }
}
 
Example 17
Source File: MainActivity.java    From MediaMetadataRetrieverCompat with MIT License 4 votes vote down vote up
private void checkPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PermissionChecker.PERMISSION_GRANTED ||
            ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PermissionChecker.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
    }
}
 
Example 18
Source File: MainActivity.java    From YAAB with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == READ_PERMISSION_REQUEST && grantResults[0] == PermissionChecker.PERMISSION_GRANTED)
        presenter.getInteractor().scanForNewAudioBooks();
}
 
Example 19
Source File: OverlayViewManager.java    From DebugOverlay-Android with Apache License 2.0 4 votes vote down vote up
public static boolean hasSystemAlertPermission(@NonNull Context context) {
    return PermissionChecker.checkSelfPermission(context, Manifest.permission.SYSTEM_ALERT_WINDOW)
            == PermissionChecker.PERMISSION_GRANTED;
}
 
Example 20
Source File: PermissMeUtils.java    From PermissMe with Apache License 2.0 3 votes vote down vote up
/**
 * Checks if the permission is invalid (null) or if it's not, if the package has the parameter permission already
 * granted
 *
 * @param context
 * 		the context
 * @param permission
 * 		permission to check
 * @return {@code true} if the permission is null or empty or the package has the permission already,
 * {@code false} if the permission is ungranted for the package
 */
@VisibleForTesting
/*package*/ static boolean permissionIsInvalidOrHasPermission(@NonNull final Context context,
                                                              final String permission) {
	return permission == null
			|| permission.isEmpty()
			|| PermissionChecker.checkSelfPermission(context, permission) == PermissionChecker.PERMISSION_GRANTED;
}