BaseActivity.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package com.github.tvbox.osc.base;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.content.res.AssetManager;
  5. import android.content.res.Resources;
  6. import android.graphics.BitmapFactory;
  7. import android.graphics.drawable.BitmapDrawable;
  8. import android.os.Build;
  9. import android.os.Bundle;
  10. import android.os.Looper;
  11. import android.util.DisplayMetrics;
  12. import android.view.View;
  13. import androidx.annotation.Nullable;
  14. import androidx.appcompat.app.AppCompatActivity;
  15. import androidx.core.content.PermissionChecker;
  16. import com.github.tvbox.osc.R;
  17. import com.github.tvbox.osc.callback.EmptyCallback;
  18. import com.github.tvbox.osc.callback.LoadingCallback;
  19. import com.github.tvbox.osc.util.AppManager;
  20. import com.kingja.loadsir.callback.Callback;
  21. import com.kingja.loadsir.core.LoadService;
  22. import com.kingja.loadsir.core.LoadSir;
  23. import java.io.BufferedReader;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.io.InputStreamReader;
  27. import me.jessyan.autosize.AutoSizeCompat;
  28. import me.jessyan.autosize.internal.CustomAdapt;
  29. /**
  30. * @author pj567
  31. * @date :2020/12/17
  32. * @description:
  33. */
  34. public abstract class BaseActivity extends AppCompatActivity implements CustomAdapt {
  35. protected Context mContext;
  36. private LoadService mLoadService;
  37. private static float screenRatio = -100.0f;
  38. @Override
  39. protected void onCreate(@Nullable Bundle savedInstanceState) {
  40. try {
  41. if (screenRatio < 0) {
  42. DisplayMetrics dm = new DisplayMetrics();
  43. getWindowManager().getDefaultDisplay().getMetrics(dm);
  44. int screenWidth = dm.widthPixels;
  45. int screenHeight = dm.heightPixels;
  46. screenRatio = (float) Math.max(screenWidth, screenHeight) / (float) Math.min(screenWidth, screenHeight);
  47. }
  48. } catch (Throwable th) {
  49. th.printStackTrace();
  50. }
  51. super.onCreate(savedInstanceState);
  52. setContentView(getLayoutResID());
  53. mContext = this;
  54. AppManager.getInstance().addActivity(this);
  55. init();
  56. }
  57. @Override
  58. protected void onResume() {
  59. super.onResume();
  60. hideSysBar();
  61. changeWallpaper(false);
  62. }
  63. public void hideSysBar() {
  64. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  65. int uiOptions = getWindow().getDecorView().getSystemUiVisibility();
  66. uiOptions |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
  67. uiOptions |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
  68. uiOptions |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
  69. uiOptions |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
  70. uiOptions |= View.SYSTEM_UI_FLAG_FULLSCREEN;
  71. uiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
  72. getWindow().getDecorView().setSystemUiVisibility(uiOptions);
  73. }
  74. }
  75. @Override
  76. public Resources getResources() {
  77. if (Looper.myLooper() == Looper.getMainLooper()) {
  78. AutoSizeCompat.autoConvertDensityOfCustomAdapt(super.getResources(), this);
  79. }
  80. return super.getResources();
  81. }
  82. public boolean hasPermission(String permission) {
  83. boolean has = true;
  84. try {
  85. has = PermissionChecker.checkSelfPermission(this, permission) == PermissionChecker.PERMISSION_GRANTED;
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. }
  89. return has;
  90. }
  91. protected abstract int getLayoutResID();
  92. protected abstract void init();
  93. protected void setLoadSir(View view) {
  94. if (mLoadService == null) {
  95. mLoadService = LoadSir.getDefault().register(view, new Callback.OnReloadListener() {
  96. @Override
  97. public void onReload(View v) {
  98. }
  99. });
  100. }
  101. }
  102. protected void showLoading() {
  103. if (mLoadService != null) {
  104. mLoadService.showCallback(LoadingCallback.class);
  105. }
  106. }
  107. protected void showEmpty() {
  108. if (null != mLoadService) {
  109. mLoadService.showCallback(EmptyCallback.class);
  110. }
  111. }
  112. protected void showSuccess() {
  113. if (null != mLoadService) {
  114. mLoadService.showSuccess();
  115. }
  116. }
  117. @Override
  118. protected void onDestroy() {
  119. super.onDestroy();
  120. AppManager.getInstance().finishActivity(this);
  121. }
  122. public void jumpActivity(Class<? extends BaseActivity> clazz) {
  123. Intent intent = new Intent(mContext, clazz);
  124. startActivity(intent);
  125. }
  126. public void jumpActivity(Class<? extends BaseActivity> clazz, Bundle bundle) {
  127. Intent intent = new Intent(mContext, clazz);
  128. intent.putExtras(bundle);
  129. startActivity(intent);
  130. }
  131. protected String getAssetText(String fileName) {
  132. StringBuilder stringBuilder = new StringBuilder();
  133. try {
  134. AssetManager assets = getAssets();
  135. BufferedReader bf = new BufferedReader(new InputStreamReader(assets.open(fileName)));
  136. String line;
  137. while ((line = bf.readLine()) != null) {
  138. stringBuilder.append(line);
  139. }
  140. return stringBuilder.toString();
  141. } catch (IOException e) {
  142. e.printStackTrace();
  143. }
  144. return "";
  145. }
  146. public boolean supportsPiPMode() {
  147. return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
  148. }
  149. @Override
  150. public float getSizeInDp() {
  151. return isBaseOnWidth() ? 1280 : 720;
  152. }
  153. @Override
  154. public boolean isBaseOnWidth() {
  155. return !(screenRatio >= 4.0f);
  156. }
  157. protected static BitmapDrawable globalWp = null;
  158. public void changeWallpaper(boolean force) {
  159. if (!force && globalWp != null)
  160. getWindow().setBackgroundDrawable(globalWp);
  161. try {
  162. File wp = new File(getFilesDir().getAbsolutePath() + "/wp");
  163. if (wp.exists()) {
  164. BitmapFactory.Options opts = new BitmapFactory.Options();
  165. opts.inJustDecodeBounds = true;
  166. BitmapFactory.decodeFile(wp.getAbsolutePath(), opts);
  167. // 从Options中获取图片的分辨率
  168. int imageHeight = opts.outHeight;
  169. int imageWidth = opts.outWidth;
  170. int picHeight = 720;
  171. int picWidth = 1080;
  172. int scaleX = imageWidth / picWidth;
  173. int scaleY = imageHeight / picHeight;
  174. int scale = 1;
  175. if (scaleX > scaleY && scaleY >= 1) {
  176. scale = scaleX;
  177. }
  178. if (scaleX < scaleY && scaleX >= 1) {
  179. scale = scaleY;
  180. }
  181. opts.inJustDecodeBounds = false;
  182. // 采样率
  183. opts.inSampleSize = scale;
  184. globalWp = new BitmapDrawable(BitmapFactory.decodeFile(wp.getAbsolutePath(), opts));
  185. } else {
  186. globalWp = null;
  187. }
  188. } catch (Throwable throwable) {
  189. throwable.printStackTrace();
  190. globalWp = null;
  191. }
  192. if (globalWp != null)
  193. getWindow().setBackgroundDrawable(globalWp);
  194. else
  195. getWindow().setBackgroundDrawableResource(R.drawable.app_bg);
  196. }
  197. }