150 lines
5.0 KiB
Java
Raw Normal View History

2017-02-07 02:01:32 +08:00
package com.topjohnwu.magisk.components;
import android.Manifest;
2018-08-01 14:16:44 +08:00
import android.content.Context;
2018-07-28 22:52:40 +08:00
import android.content.Intent;
import android.content.pm.PackageManager;
2018-07-30 20:37:00 +08:00
import android.content.res.Configuration;
2017-07-23 00:12:15 +08:00
import android.os.Bundle;
2017-05-24 21:21:15 +02:00
import android.view.WindowManager;
import android.widget.Toast;
2017-02-07 02:01:32 +08:00
import com.topjohnwu.core.App;
2019-01-21 15:49:03 -05:00
import com.topjohnwu.core.Config;
import com.topjohnwu.core.Const;
import com.topjohnwu.core.utils.LocaleManager;
import com.topjohnwu.core.utils.Topic;
2017-05-24 21:21:15 +02:00
import com.topjohnwu.magisk.R;
2017-02-07 02:01:32 +08:00
import androidx.annotation.NonNull;
2018-09-10 02:27:45 -04:00
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.appcompat.app.AppCompatActivity;
2018-12-28 05:15:59 +08:00
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
2018-09-10 02:27:45 -04:00
public abstract class BaseActivity extends AppCompatActivity implements Topic.AutoSubscriber {
2017-02-07 02:01:32 +08:00
public static final String INTENT_PERM = "perm_dialog";
private static Runnable grantCallback;
2018-08-01 00:47:31 +08:00
static int[] EMPTY_INT_ARRAY = new int[0];
2018-07-28 22:52:40 +08:00
private ActivityResultListener activityResultListener;
2018-12-13 05:53:39 -05:00
public App app = App.self;
2018-07-30 20:37:00 +08:00
2018-12-28 05:15:59 +08:00
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
2018-08-01 00:47:31 +08:00
@Override
public int[] getSubscribedTopics() {
return EMPTY_INT_ARRAY;
}
2017-12-03 15:15:00 +08:00
@StyleRes
2018-03-28 02:36:03 +08:00
public int getDarkTheme() {
return -1;
}
2017-12-03 15:15:00 +08:00
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
Configuration config = base.getResources().getConfiguration();
config.setLocale(LocaleManager.locale);
applyOverrideConfiguration(config);
}
2017-07-23 00:12:15 +08:00
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
2018-08-01 00:47:31 +08:00
Topic.subscribe(this);
2019-01-21 15:49:03 -05:00
if (getDarkTheme() != -1 && (boolean) Config.get(Config.Key.DARK_THEME)) {
2017-12-03 15:15:00 +08:00
setTheme(getDarkTheme());
2017-12-03 02:48:21 +08:00
}
2018-09-17 23:29:38 -04:00
super.onCreate(savedInstanceState);
String[] perms = getIntent().getStringArrayExtra(INTENT_PERM);
if (perms != null)
ActivityCompat.requestPermissions(this, perms, 0);
2017-07-23 00:12:15 +08:00
}
@Override
protected void onDestroy() {
2018-08-01 00:47:31 +08:00
Topic.unsubscribe(this);
2017-07-23 00:12:15 +08:00
super.onDestroy();
}
2017-05-24 21:21:15 +02:00
protected void setFloating() {
boolean isTablet = getResources().getBoolean(R.bool.isTablet);
if (isTablet) {
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = getResources().getDimensionPixelSize(R.dimen.floating_height);
params.width = getResources().getDimensionPixelSize(R.dimen.floating_width);
params.alpha = 1.0f;
params.dimAmount = 0.6f;
params.flags |= 2;
getWindow().setAttributes(params);
setFinishOnTouchOutside(true);
}
}
2018-07-28 22:52:40 +08:00
public void runWithExternalRW(Runnable callback) {
runWithPermission(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, callback);
}
public void runWithPermission(String[] permissions, Runnable callback) {
runWithPermission(this, permissions, callback);
}
public static void runWithPermission(Context context, String[] permissions, Runnable callback) {
boolean granted = true;
for (String perm : permissions) {
if (ContextCompat.checkSelfPermission(context, perm) != PackageManager.PERMISSION_GRANTED)
granted = false;
}
if (granted) {
2018-12-02 15:28:18 -05:00
Const.EXTERNAL_PATH.mkdirs();
callback.run();
} else {
// Passed in context should be an activity if not granted, need to show dialog!
if (context instanceof BaseActivity) {
grantCallback = callback;
ActivityCompat.requestPermissions((BaseActivity) context, permissions, 0);
}
}
}
2018-07-28 22:52:40 +08:00
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (activityResultListener != null)
activityResultListener.onActivityResult(requestCode, resultCode, data);
activityResultListener = null;
}
public void startActivityForResult(Intent intent, int requestCode, ActivityResultListener listener) {
activityResultListener = listener;
super.startActivityForResult(intent, requestCode);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
boolean grant = true;
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED)
grant = false;
}
if (grant) {
if (grantCallback != null) {
grantCallback.run();
}
} else {
Toast.makeText(this, R.string.no_rw_storage, Toast.LENGTH_LONG).show();
}
grantCallback = null;
}
2018-07-28 22:52:40 +08:00
public interface ActivityResultListener {
void onActivityResult(int requestCode, int resultCode, Intent data);
}
2017-02-07 02:01:32 +08:00
}