123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- package com.github.tvbox.osc.base;
- import android.content.Context;
- import android.content.Intent;
- import android.content.res.AssetManager;
- import android.content.res.Resources;
- import android.graphics.BitmapFactory;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Looper;
- import android.util.DisplayMetrics;
- import android.view.View;
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.content.PermissionChecker;
- import com.github.tvbox.osc.R;
- import com.github.tvbox.osc.callback.EmptyCallback;
- import com.github.tvbox.osc.callback.LoadingCallback;
- import com.github.tvbox.osc.util.AppManager;
- import com.kingja.loadsir.callback.Callback;
- import com.kingja.loadsir.core.LoadService;
- import com.kingja.loadsir.core.LoadSir;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import me.jessyan.autosize.AutoSizeCompat;
- import me.jessyan.autosize.internal.CustomAdapt;
- /**
- * @author pj567
- * @date :2020/12/17
- * @description:
- */
- public abstract class BaseActivity extends AppCompatActivity implements CustomAdapt {
- protected Context mContext;
- private LoadService mLoadService;
- private static float screenRatio = -100.0f;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- try {
- if (screenRatio < 0) {
- DisplayMetrics dm = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- int screenWidth = dm.widthPixels;
- int screenHeight = dm.heightPixels;
- screenRatio = (float) Math.max(screenWidth, screenHeight) / (float) Math.min(screenWidth, screenHeight);
- }
- } catch (Throwable th) {
- th.printStackTrace();
- }
- super.onCreate(savedInstanceState);
- setContentView(getLayoutResID());
- mContext = this;
- AppManager.getInstance().addActivity(this);
- init();
- }
- @Override
- protected void onResume() {
- super.onResume();
- hideSysBar();
- changeWallpaper(false);
- }
- public void hideSysBar() {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- int uiOptions = getWindow().getDecorView().getSystemUiVisibility();
- uiOptions |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
- uiOptions |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
- uiOptions |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
- uiOptions |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
- uiOptions |= View.SYSTEM_UI_FLAG_FULLSCREEN;
- uiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
- getWindow().getDecorView().setSystemUiVisibility(uiOptions);
- }
- }
- @Override
- public Resources getResources() {
- if (Looper.myLooper() == Looper.getMainLooper()) {
- AutoSizeCompat.autoConvertDensityOfCustomAdapt(super.getResources(), this);
- }
- return super.getResources();
- }
- public boolean hasPermission(String permission) {
- boolean has = true;
- try {
- has = PermissionChecker.checkSelfPermission(this, permission) == PermissionChecker.PERMISSION_GRANTED;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return has;
- }
- protected abstract int getLayoutResID();
- protected abstract void init();
- protected void setLoadSir(View view) {
- if (mLoadService == null) {
- mLoadService = LoadSir.getDefault().register(view, new Callback.OnReloadListener() {
- @Override
- public void onReload(View v) {
- }
- });
- }
- }
- protected void showLoading() {
- if (mLoadService != null) {
- mLoadService.showCallback(LoadingCallback.class);
- }
- }
- protected void showEmpty() {
- if (null != mLoadService) {
- mLoadService.showCallback(EmptyCallback.class);
- }
- }
- protected void showSuccess() {
- if (null != mLoadService) {
- mLoadService.showSuccess();
- }
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- AppManager.getInstance().finishActivity(this);
- }
- public void jumpActivity(Class<? extends BaseActivity> clazz) {
- Intent intent = new Intent(mContext, clazz);
- startActivity(intent);
- }
- public void jumpActivity(Class<? extends BaseActivity> clazz, Bundle bundle) {
- Intent intent = new Intent(mContext, clazz);
- intent.putExtras(bundle);
- startActivity(intent);
- }
- protected String getAssetText(String fileName) {
- StringBuilder stringBuilder = new StringBuilder();
- try {
- AssetManager assets = getAssets();
- BufferedReader bf = new BufferedReader(new InputStreamReader(assets.open(fileName)));
- String line;
- while ((line = bf.readLine()) != null) {
- stringBuilder.append(line);
- }
- return stringBuilder.toString();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return "";
- }
- public boolean supportsPiPMode() {
- return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
- }
-
- @Override
- public float getSizeInDp() {
- return isBaseOnWidth() ? 1280 : 720;
- }
- @Override
- public boolean isBaseOnWidth() {
- return !(screenRatio >= 4.0f);
- }
- protected static BitmapDrawable globalWp = null;
- public void changeWallpaper(boolean force) {
- if (!force && globalWp != null)
- getWindow().setBackgroundDrawable(globalWp);
- try {
- File wp = new File(getFilesDir().getAbsolutePath() + "/wp");
- if (wp.exists()) {
- BitmapFactory.Options opts = new BitmapFactory.Options();
- opts.inJustDecodeBounds = true;
- BitmapFactory.decodeFile(wp.getAbsolutePath(), opts);
- // 从Options中获取图片的分辨率
- int imageHeight = opts.outHeight;
- int imageWidth = opts.outWidth;
- int picHeight = 720;
- int picWidth = 1080;
- int scaleX = imageWidth / picWidth;
- int scaleY = imageHeight / picHeight;
- int scale = 1;
- if (scaleX > scaleY && scaleY >= 1) {
- scale = scaleX;
- }
- if (scaleX < scaleY && scaleX >= 1) {
- scale = scaleY;
- }
- opts.inJustDecodeBounds = false;
- // 采样率
- opts.inSampleSize = scale;
- globalWp = new BitmapDrawable(BitmapFactory.decodeFile(wp.getAbsolutePath(), opts));
- } else {
- globalWp = null;
- }
- } catch (Throwable throwable) {
- throwable.printStackTrace();
- globalWp = null;
- }
- if (globalWp != null)
- getWindow().setBackgroundDrawable(globalWp);
- else
- getWindow().setBackgroundDrawableResource(R.drawable.app_bg);
- }
- }
|