From 024fabb9b4098b3842019d496b0fb15fec0191a0 Mon Sep 17 00:00:00 2001 From: Runt <qingingrunt2010@qq.com> Date: Sun, 14 Aug 2022 07:11:23 +0000 Subject: [PATCH] README.md 完善 --- app/src/main/java/com/runt/open/mvvm/base/activities/BaseActivity.java | 352 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 328 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/runt/open/mvvm/base/activities/BaseActivity.java b/app/src/main/java/com/runt/open/mvvm/base/activities/BaseActivity.java index ffe24f5..5610e15 100644 --- a/app/src/main/java/com/runt/open/mvvm/base/activities/BaseActivity.java +++ b/app/src/main/java/com/runt/open/mvvm/base/activities/BaseActivity.java @@ -3,49 +3,60 @@ import android.Manifest; import android.app.AlertDialog; import android.content.Context; +import android.content.Intent; import android.content.pm.ActivityInfo; +import android.graphics.drawable.Drawable; +import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Environment; -import android.view.LayoutInflater; -import android.view.MotionEvent; -import android.view.View; -import android.view.WindowManager; +import android.util.Log; +import android.view.*; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; +import android.widget.TextView; import android.widget.Toast; - import androidx.annotation.ColorRes; import androidx.annotation.Nullable; import androidx.annotation.StringRes; import androidx.appcompat.app.AppCompatActivity; import androidx.lifecycle.ViewModelProvider; import androidx.viewbinding.ViewBinding; - +import com.permissionx.guolindev.PermissionX; import com.runt.open.mvvm.MyApplication; import com.runt.open.mvvm.R; import com.runt.open.mvvm.base.model.BaseViewModel; import com.runt.open.mvvm.base.model.ViewModelFactory; +import com.runt.open.mvvm.listener.ResPonse; +import com.runt.open.mvvm.util.PreferencesUtils; +import com.runt.open.mvvm.widgets.TitleBarView; +import dmax.dialog.SpotsDialog; import java.io.File; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; - -import dmax.dialog.SpotsDialog; +import java.util.Set; /** * activity 封装 * Created by Administrator on 2021/10/27 0027. */ -public abstract class BaseActivity<B extends ViewBinding,VM extends BaseViewModel> extends AppCompatActivity { +public abstract class BaseActivity<VB extends ViewBinding,VM extends BaseViewModel> extends AppCompatActivity { - protected B binding; - protected VM viewModel; + //viewbind + protected VB mBinding; + //viewmodel + protected VM mViewModel; protected String TAG ; + //文件读取权限 public final String[] FILE_PERMISSIONS = new String []{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}; - public final String[] LOCATION_PERMISSIONS = new String []{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION}; + //夹带摄像头权限 public final String[] CAMERA_PERMISSIONS = new String[]{ FILE_PERMISSIONS[0],FILE_PERMISSIONS[1], Manifest.permission.CAMERA}; + //夹带录音权限 public final String[] CAMERA_RECORD_PERMISSIONS = new String[]{ FILE_PERMISSIONS[0],FILE_PERMISSIONS[1], Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO}; + + public static final String PARAMS_TITLE = "title"; + public static final String PARAMS_URL = "url"; public static final int RESULT_LOGIN = 100,RESULT_LOGIN_RECREATE = 103,RESULT_BIND = 101,RESULT_SENDEDFILES = 105,RESULT_DISSCONNECT = 104, RESULT_UPDATEUSER = 115,RESULT_LOGOUT = 113, REQUEST_CODE_ACTIVITY = 333; @@ -76,25 +87,30 @@ RESULT_CODE_SUCESS = 4046/*成功*/, RESULT_CODE_CANCEL = 4043/*取消*/; protected Context mContext; + //标题栏 + protected TitleBarView titleBarView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); // get genericity "B" - setStatusBarBgColor(R.color.white); - setStatusBarTextColor(true); + setStatusBarBgColor(R.color.white);//白色状态栏 + setStatusBarTextColor(true);//状态栏文本黑色 final ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass(); try { - Class<B> entityClass = (Class<B>) type.getActualTypeArguments()[0]; + //实例化泛型类 + Class<VB> entityClass = (Class<VB>) type.getActualTypeArguments()[0]; Method method = entityClass.getMethod("inflate", LayoutInflater.class);//get method from name "inflate"; - binding = (B) method.invoke(entityClass,getLayoutInflater());//execute method to create a objct of viewbind; + mBinding = (VB) method.invoke(entityClass,getLayoutInflater());//execute method to create a objct of viewbind; + titleBarView = (TitleBarView) mBinding.getClass().getDeclaredField("titleBar").get(mBinding); + titleBarView.setLeftClick(v -> onBackPressed()); } catch (Exception e) { e.printStackTrace(); } Class<VM> vmClass = (Class<VM>) type.getActualTypeArguments()[1]; - viewModel = new ViewModelProvider(this,getViewModelFactory()).get(vmClass); - setContentView(binding.getRoot()); + mViewModel = new ViewModelProvider(this,getViewModelFactory()).get(vmClass); + setContentView(mBinding.getRoot()); mContext = this; try { //设置坚屏 一定要放到try catch里面,否则会崩溃 @@ -102,16 +118,154 @@ } catch (Exception e) { } TAG = this.getClass().getSimpleName(); - initViews(); + initViews();//初始化UI + mViewModel.onCreate(this); + loadData();//加载数据 } public abstract void initViews(); + public abstract void loadData(); + /** + * 是否为空 + * @param object + * @return + */ public boolean isNull(Object object){ return object == null || object.toString().trim().equals("") || object.equals("null"); } + /** + * 设置标题 + * @param text + */ + protected void setTitle(String text){ + titleBarView.setTitleText(text); + } + + /** + * 标题栏右侧文本 + * @param text + */ + protected void setTitleRight(String text){ + titleBarView.setRightText(text); + titleBarView.setRightDra(null); + } + + /** + * 标题栏右侧图片 + * @param drawable + */ + protected void setTitleRight(Drawable drawable){ + titleBarView.setRightText(null); + titleBarView.setRightDra(drawable); + } + + + AlertDialog dialog; + /** + * 显示输入框弹框 + * @param title 标题 + * @param msg 消息 + * @param resPonse 回调 + */ + public void showDialog(String title,String msg,ResPonse resPonse){ + showDialog(title,msg,"确认","取消",resPonse); + } + + /** + * 显示输入框弹框 + * @param title 标题 + * @param msg 消息 + * @param hint 默认消息文本 + * @param resPonse 回调 + */ + public void showInputDialog(String title,String msg,String hint,ResPonse resPonse){ + showInputDialog(title,msg,hint,"确认","取消",resPonse); + } + + /** + * 显示输入框弹框 + * @param title 标题 + * @param msg 消息 + * @param hint 默认消息文本 + * @param btnOk 确认按钮文本 + * @param btnCancel 取消按钮文本 + * @param resPonse 回调 + */ + public void showInputDialog(String title, String msg, String hint,String btnOk,String btnCancel,final ResPonse resPonse){ + showDialog(title,msg,hint,btnOk,btnCancel,resPonse,true); + } + + /** + * 显示弹框 + * @param title 标题 + * @param msg 消息 + * @param btnOk 确认按钮文本 + * @param btnCancel 取消按钮文本 + * @param resPonse 回调 + */ + public void showDialog(String title, String msg, String btnOk,String btnCancel,final ResPonse resPonse){ + showDialog(title,msg,null,btnOk,btnCancel,resPonse,false); + } + + /** + * 显示弹框 + * @param title 标题 + * @param msg 消息 + * @param hint 默认消息文本 + * @param btnOk 确认按钮文本 + * @param btnCancel 取消按钮文本 + * @param resPonse 回调 + * @param isEdit 是否为输入框 + */ + private void showDialog(String title, String msg, String hint,String btnOk,String btnCancel,final ResPonse resPonse,boolean isEdit){ + + AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.TransparentDialog); + builder.setCancelable(false); + final View view = LayoutInflater.from(this).inflate(R.layout.layout_dialog,null); + TextView titleView = view.findViewById(R.id.txt_title); + TextView cancelView = view.findViewById(R.id.txt_cancel); + final TextView textView = view.findViewById(R.id.msg); + if(isEdit){ + textView.setEnabled(true); + }else{ + textView.setBackground(null); + } + cancelView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + dialog.dismiss(); + if(resPonse !=null){ + resPonse.doError(null); + } + } + }); + cancelView.setText(btnCancel); + TextView confirmView = view.findViewById(R.id.txt_confirm); + confirmView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + dialog.dismiss(); + if(resPonse !=null){ + resPonse.doSuccess(textView.getText().toString()); + } + } + }); + confirmView.setText(btnOk); + confirmView.requestFocus(); + if(hint != null){ + textView.setHint(hint); + } + if(msg != null){ + textView.setText(msg); + } + titleView.setText(title); + builder.setView(view); + dialog = builder.create(); + dialog.show(); + } AlertDialog loadingDialog; /** @@ -137,15 +291,27 @@ } } + /** + * viewmodel 工厂创建实例 + * @return + */ public ViewModelProvider.Factory getViewModelFactory(){ return ViewModelFactory.getInstance(); } + /** + * 透明状态栏 + * @param isBlack 是否为黑色文本 + */ public void setStatusBarTransparent(boolean isBlack){ //透明状态栏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明导航栏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); + setStatusBarTextColor(isBlack); + final ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) titleBarView.getLayoutParams(); + layoutParams.topMargin = layoutParams.topMargin+getStatusBarHeight(); + titleBarView.setLayoutParams(layoutParams); } /** @@ -274,7 +440,8 @@ } } public void showToast(String message){ - Toast.makeText(this,message,Toast.LENGTH_SHORT).show(); + Log.i(TAG,"showToast "+message); + runOnUiThread(() -> Toast.makeText(mContext,message,Toast.LENGTH_SHORT).show()); } public void showToast(@StringRes int msg){ @@ -296,10 +463,147 @@ return storePath + File.separator +fileName; } - - protected boolean onBackKeyDown() { - onBackPressed(); - return false; + /** + * 拨打电话(直接拨打电话) + * @param phoneNum 电话号码 + */ + public void callPhone(String phoneNum){ + PermissionX.init(this) + .permissions(Manifest.permission.CALL_PHONE) + .request((allGranted, grantedList, deniedList) -> { + if(allGranted){ + Intent intent = new Intent(Intent.ACTION_CALL); + Uri data = Uri.parse("tel:" + phoneNum); + intent.setData(data); + startActivity(intent); + }else{ + showToast("权限被拒绝"); + } + }); } + //=========================================== + //以下为preferences操作 + public boolean getBooleanUserPrefrence(String key){ + return PreferencesUtils.getBoolean(this,key,false,PreferencesUtils.USER); + } + public boolean getBooleanProjectPrefrence(String key){ + return PreferencesUtils.getBoolean(this,key,false,PreferencesUtils.PROJECT); + } + + public String getStringUserPrefrence(String key){ + return PreferencesUtils.getString(this,key,"",PreferencesUtils.USER); + } + + public String getStringProjectPrefrence(String key){ + return PreferencesUtils.getString(this,key,"",PreferencesUtils.PROJECT); + } + + public Integer getIntProjectPrefrence(String key){ + return PreferencesUtils.getInt(this,key,0,PreferencesUtils.PROJECT); + } + + public Long getLongProjectPrefrence(String key){ + return PreferencesUtils.getLong(this,key,0,PreferencesUtils.PROJECT); + } + + public float getFloatProjectPrefrence(String key){ + return PreferencesUtils.getFloat(this,key,0,PreferencesUtils.PROJECT); + } + + public Set getStringSetProjectPrefrence(String key){ + return PreferencesUtils.getStringSet(this,key,PreferencesUtils.PROJECT); + } + + public Integer getIntUserPrefrence(String key){ + return PreferencesUtils.getInt(this,key,0,PreferencesUtils.USER); + } + + public Long getLongUserPrefrence(String key){ + return PreferencesUtils.getLong(this,key,0,PreferencesUtils.USER); + } + + public float getFloatUserPrefrence(String key){ + return PreferencesUtils.getFloat(this,key,0,PreferencesUtils.USER); + } + + public Set getStringSetUserPrefrence(String key){ + return PreferencesUtils.getStringSet(this,key,PreferencesUtils.USER); + } + + + public void putBooleanUserPrefrence(String key ,Boolean value){ + PreferencesUtils.putBoolean(this,key,value,PreferencesUtils.USER); + } + + public void putBooleanProjectPrefrence(String key,Boolean value){ + PreferencesUtils.putBoolean(this,key,value,PreferencesUtils.PROJECT); + } + + public void putStringUserPrefrence(String key,String value){ + PreferencesUtils.putString(this,key,value,PreferencesUtils.USER); + } + + public void putStringProjectPrefrence(String key,String value){ + PreferencesUtils.putString(this,key,value,PreferencesUtils.PROJECT); + } + + public void putIntProjectPrefrence(String key,int value){ + PreferencesUtils.putInt(this,key,value,PreferencesUtils.PROJECT); + } + + public void putLongProjectPrefrence(String key,long value){ + PreferencesUtils.putLong(this,key,value,PreferencesUtils.PROJECT); + } + + public void putFloatProjectPrefrence(String key,float value){ + PreferencesUtils.putFloat(this,key,value,PreferencesUtils.PROJECT); + } + + public void putStringSetProjectPrefrence(String key, Set value){ + PreferencesUtils.putStringSet(this,key,value,PreferencesUtils.PROJECT); + } + + public void putIntUserPrefrence(String key,int value){ + PreferencesUtils.putInt(this,key,value,PreferencesUtils.USER); + } + + public void putLongUserPrefrence(String key,long value){ + PreferencesUtils.putLong(this,key,value,PreferencesUtils.USER); + } + + public void putFloatUserPrefrence(String key,float value){ + PreferencesUtils.putFloat(this,key,value,PreferencesUtils.USER); + } + + public void putStringSetUserPrefrence(String key, Set value){ + PreferencesUtils.putStringSet(this,key,value,PreferencesUtils.USER); + } + + + public void removeUserKey(String key){ + PreferencesUtils.removeKey(this,key,PreferencesUtils.USER); + } + + public void removeProjectKey(String key){ + PreferencesUtils.removeKey(this,key,PreferencesUtils.PROJECT); + } + + public void removeUserValue(String Value){ + PreferencesUtils.removeValue(this,Value,PreferencesUtils.USER); + } + + public void removeProjectValue(String Value){ + PreferencesUtils.removeValue(this,Value,PreferencesUtils.PROJECT); + } + + + public void clearProjectData(){ + PreferencesUtils.clearData(this,PreferencesUtils.PROJECT); + } + public void clearUserData(){ + PreferencesUtils.clearData(this,PreferencesUtils.USER); + } + + } -- Gitblit v1.9.1