Skip to content

Instantly share code, notes, and snippets.

@shaobin0604
Created June 12, 2018 12:20
Show Gist options
  • Save shaobin0604/3dfa249c6e1c2cf9f3352e22830ce2e1 to your computer and use it in GitHub Desktop.
Save shaobin0604/3dfa249c6e1c2cf9f3352e22830ce2e1 to your computer and use it in GitHub Desktop.
RuntimeSettingPage from AndPermission
/*
* Copyright 2018 Yan Zhenjie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yanzhenjie.permission.runtime.setting;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import com.yanzhenjie.permission.source.Source;
/**
* Created by YanZhenjie on 2018/4/30.
*/
public class RuntimeSettingPage {
private static final String MARK = Build.MANUFACTURER.toLowerCase();
private Source mSource;
public RuntimeSettingPage(Source source) {
this.mSource = source;
}
/**
* Start.
*
* @param requestCode this code will be returned in onActivityResult() when the activity exits.
* @return true if successful, otherwise is false.
*/
public void start(int requestCode) {
Intent intent;
if (MARK.contains("huawei")) {
intent = huaweiApi(mSource.getContext());
} else if (MARK.contains("xiaomi")) {
intent = xiaomiApi(mSource.getContext());
} else if (MARK.contains("oppo")) {
intent = oppoApi(mSource.getContext());
} else if (MARK.contains("vivo")) {
intent = vivoApi(mSource.getContext());
} else if (MARK.contains("meizu")) {
intent = meizuApi(mSource.getContext());
} else {
intent = defaultApi(mSource.getContext());
}
try {
mSource.startActivityForResult(intent, requestCode);
} catch (Exception e) {
intent = defaultApi(mSource.getContext());
mSource.startActivityForResult(intent, requestCode);
}
}
private static Intent defaultApi(Context context) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.fromParts("package", context.getPackageName(), null));
return intent;
}
private static Intent huaweiApi(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return defaultApi(context);
}
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity"));
return intent;
}
private static Intent xiaomiApi(Context context) {
Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
intent.putExtra("extra_pkgname", context.getPackageName());
return intent;
}
private static Intent vivoApi(Context context) {
Intent intent = new Intent();
intent.putExtra("packagename", context.getPackageName());
intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.SoftPermissionDetailActivity"));
return intent;
}
private static Intent oppoApi(Context context) {
Intent intent = new Intent();
intent.putExtra("packageName", context.getPackageName());
intent.setComponent(new ComponentName("com.color.safecenter", "com.color.safecenter.permission.PermissionManagerActivity"));
return intent;
}
private static Intent meizuApi(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return defaultApi(context);
}
Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
intent.putExtra("packageName", context.getPackageName());
intent.setComponent(new ComponentName("com.meizu.safe", "com.meizu.safe.security.AppSecActivity"));
return intent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment