Example usage for android.content Context checkCallingOrSelfPermission

List of usage examples for android.content Context checkCallingOrSelfPermission

Introduction

In this page you can find the example usage for android.content Context checkCallingOrSelfPermission.

Prototype

@CheckResult(suggest = "#enforceCallingOrSelfPermission(String,String)")
@PackageManager.PermissionResult
public abstract int checkCallingOrSelfPermission(@NonNull String permission);

Source Link

Document

Determine whether the calling process of an IPC or you have been granted a particular permission.

Usage

From source file:com.kaixin.connect.Kaixin.java

/**
 * access_token(User-Agent Flow)//  ww w .ja v a2s.  c  o m
 * 
 * @param context
 * @param permissions
 *            http://wiki.open.kaixin001.com/index.php?id=OAuth%E6%96%
 *            87%E6%A1%A3#REST%E6%
 *            8E%A5%E5%8F%A3%E5%92%8COAuth%E6%9D%83%E9%99%90%E5%AF%B9%E7%85%
 *            A 7%E8%A1%A8
 * @param listener
 * @param redirectUrl
 * @param responseType
 */
private void authorize(final Context context, String[] permissions, final KaixinAuthListener listener,
        final String redirectUrl, String responseType) {

    CookieSyncManager.createInstance(context);

    Bundle params = new Bundle();
    params.putString("client_id", API_KEY);
    params.putString("response_type", responseType);
    params.putString("redirect_uri", redirectUrl);
    params.putString("state", "");
    params.putString("display", "page");
    params.putString("oauth_client", "1");

    if (permissions != null && permissions.length > 0) {
        String scope = TextUtils.join(" ", permissions);
        params.putString("scope", scope);
    }

    String url = KX_AUTHORIZE_URL + "?" + Util.encodeUrl(params);
    if (context
            .checkCallingOrSelfPermission(Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
        Util.showAlert(context, "", "");
    } else {
        new KaixinDialog(context, url, new KaixinDialogListener() {
            @Override
            public int onPageBegin(String url) {
                return KaixinDialogListener.DIALOG_PROCCESS;
            }

            @Override
            public void onPageFinished(String url) {
            }

            @Override
            public boolean onPageStart(String url) {
                return (KaixinDialogListener.PROCCESSED == parseUrl(url));
            }

            @Override
            public void onReceivedError(int errorCode, String description, String failingUrl) {
                listener.onAuthError(new KaixinAuthError(String.valueOf(errorCode), description, failingUrl));
            }

            private int parseUrl(String url) {
                if (url.startsWith(KX_AUTHORIZE_CALLBACK_URL)) {
                    Bundle values = Util.parseUrl(url);
                    String error = values.getString("error");// 
                    if (error != null) {
                        if (ACCESS_DENIED.equalsIgnoreCase(error)) {
                            listener.onAuthCancel(values);
                        } else if (LOGIN_DENIED.equalsIgnoreCase(error)) {
                            listener.onAuthCancelLogin();
                        } else {
                            listener.onAuthError(new KaixinAuthError(error, error, url));
                        }

                        Util.clearCookies(context);

                        setAccessToken(null);
                        setRefreshToken(null);
                        setAccessExpires(0L);

                    } else {
                        this.authComplete(values, url);
                    }
                    return KaixinDialogListener.PROCCESSED;
                }
                return KaixinDialogListener.UNPROCCESS;
            }

            private void authComplete(Bundle values, String url) {
                CookieSyncManager.getInstance().sync();
                String accessToken = values.getString(ACCESS_TOKEN);
                String refreshToken = values.getString(REFRESH_TOKEN);
                String expiresIn = values.getString(EXPIRES_IN);
                if (accessToken != null && refreshToken != null && expiresIn != null) {
                    try {
                        setAccessToken(accessToken);
                        setRefreshToken(refreshToken);
                        setAccessExpiresIn(expiresIn);
                        listener.onAuthComplete(values);
                    } catch (Exception e) {
                        listener.onAuthError(
                                new KaixinAuthError(e.getClass().getName(), e.getMessage(), e.toString()));
                    }
                } else {
                    listener.onAuthError(new KaixinAuthError("", "", url));
                }
            }
        }).show();
    }
}

From source file:com.ztspeech.weibo.sdk.kaixin.Kaixin.java

/**
 * access_token(User-Agent Flow)//  www  .  ja v  a 2s . co m
 * 
 * @param context
 * @param permissions
 *            http://wiki.open.kaixin001.com/index.php?id=OAuth%E6%96
 *            % 87%E6%A1%A3#REST%E6%
 *            8E%A5%E5%8F%A3%E5%92%8COAuth%E6%9D%83%E9%99%90%E5%AF%B9%E7%85%
 *            A 7%E8%A1%A8
 * @param listener
 * @param redirectUrl
 * @param responseType
 */
private void authorize(final Context context, String[] permissions, final KaixinAuthListener listener,
        final String redirectUrl, String responseType) {

    CookieSyncManager.createInstance(context);

    Bundle params = new Bundle();
    params.putString("client_id", API_KEY);
    params.putString("response_type", responseType);
    params.putString("redirect_uri", redirectUrl);
    params.putString("state", "");
    params.putString("display", "page");
    params.putString("oauth_client", "1");

    if (permissions != null && permissions.length > 0) {
        String scope = TextUtils.join(" ", permissions);
        params.putString("scope", scope);
    }

    String url = KX_AUTHORIZE_URL + "?" + Util.encodeUrl(params);
    if (context
            .checkCallingOrSelfPermission(Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
        Util.showAlert(context, "", "");
    } else {
        new KaixinDialog(context, url, new KaixinDialogListener() {
            @Override
            public int onPageBegin(String url) {
                return KaixinDialogListener.DIALOG_PROCCESS;
            }

            @Override
            public void onPageFinished(String url) {
            }

            @Override
            public boolean onPageStart(String url) {
                return (KaixinDialogListener.PROCCESSED == parseUrl(url));
            }

            @Override
            public void onReceivedError(int errorCode, String description, String failingUrl) {
                listener.onAuthError(new KaixinAuthError(String.valueOf(errorCode), description, failingUrl));
            }

            private int parseUrl(String url) {
                if (url.startsWith(KX_AUTHORIZE_CALLBACK_URL)) {
                    Bundle values = Util.parseUrl(url);
                    String error = values.getString("error");// 
                    if (error != null) {
                        if (ACCESS_DENIED.equalsIgnoreCase(error)) {
                            listener.onAuthCancel(values);
                        } else if (LOGIN_DENIED.equalsIgnoreCase(error)) {
                            listener.onAuthCancelLogin();
                        } else {
                            listener.onAuthError(new KaixinAuthError(error, error, url));
                        }

                        Util.clearCookies(context);

                        setAccessToken(null);
                        setRefreshToken(null);
                        setAccessExpires(0L);

                    } else {
                        this.authComplete(values, url);
                    }
                    return KaixinDialogListener.PROCCESSED;
                }
                return KaixinDialogListener.UNPROCCESS;
            }

            private void authComplete(Bundle values, String url) {
                CookieSyncManager.getInstance().sync();
                String accessToken = values.getString(ACCESS_TOKEN);
                String refreshToken = values.getString(REFRESH_TOKEN);
                String expiresIn = values.getString(EXPIRES_IN);
                if (accessToken != null && refreshToken != null && expiresIn != null) {
                    try {
                        setAccessToken(accessToken);
                        setRefreshToken(refreshToken);
                        setAccessExpiresIn(expiresIn);
                        updateStorage(context);
                        listener.onAuthComplete(values);
                    } catch (Exception e) {
                        listener.onAuthError(
                                new KaixinAuthError(e.getClass().getName(), e.getMessage(), e.toString()));
                    }
                } else {
                    listener.onAuthError(new KaixinAuthError("", "", url));
                }
            }
        }).show();
    }
}

From source file:jp.mixi.android.sdk.MixiContainerImpl.java

private boolean validatePermission(Context context) {
    if (context
            .checkCallingOrSelfPermission(Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
        Log.w(TAG, "No permission Manifest.permission.INTERNET");
        Builder alertBuilder = new Builder(context);
        alertBuilder.setMessage(context.getString(R.string.no_permission_error));
        alertBuilder.create().show();//  www.ja  va 2  s  .  c  o m
        return false;
    }
    return true;
}

From source file:cm.aptoide.com.facebook.android.Facebook.java

/**
 * Generate a UI dialog for the request action in the given Android context
 * with the provided parameters.//from ww w .jav  a  2 s  .c om
 *
 * Note that this method is asynchronous and the callback will be invoked in
 * the original calling thread (not in a background thread).
 *
 * @param context
 *            The Android context in which we will generate this dialog.
 * @param action
 *            String representation of the desired method: e.g. "feed" ...
 * @param parameters
 *            String key-value pairs to be passed as URL parameters.
 * @param listener
 *            Callback interface to notify the application when the dialog
 *            has completed.
 */
public void dialog(Context context, String action, Bundle parameters, final DialogListener listener) {

    String endpoint = DIALOG_BASE_URL + action;
    parameters.putString("display", "touch");
    parameters.putString("redirect_uri", REDIRECT_URI);

    if (action.equals(LOGIN)) {
        parameters.putString("type", "user_agent");
        parameters.putString("client_id", mAppId);
    } else {
        parameters.putString("app_id", mAppId);
    }

    if (isSessionValid()) {
        parameters.putString(TOKEN, getAccessToken());
    }
    String url = endpoint + "?" + Util.encodeUrl(parameters);
    if (context
            .checkCallingOrSelfPermission(Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
        Util.showAlert(context, "Error", "Application requires permission to access the Internet");
    } else {
        new FbDialog(context, url, listener).show();
    }
}