Skip to content

Commit

Permalink
6.9.4 update
Browse files Browse the repository at this point in the history
- 支持 DataBinding,要使用请先在初始化时开启 `BaseFrameworkSettings.useDataBinding = true` 然后在 BaseActivity 上设置对应的 ViewBinding 泛型,例如 `MainActivity extends BaseActivity<ActivityMainBinding>` 然后直接使用 `binding.` 即可。
- Permission 新增媒体照片权限 MEDIA(),也支持使用 add 动态添加权限;
  • Loading branch information
kongzue committed Feb 1, 2024
1 parent b966bdc commit 7c7a5cf
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 14 deletions.
9 changes: 5 additions & 4 deletions baseframework/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ apply plugin: 'com.android.library'
//apply from: "${rootProject.projectDir}/publish-mavencentral.gradle"

android {
compileSdkVersion 30
compileSdkVersion 34

defaultConfig {
minSdkVersion 15
targetSdkVersion 30
versionCode 132
versionName "6.9.3"
targetSdkVersion 34
versionCode 133
versionName "6.9.4"
}

buildTypes {
Expand All @@ -36,4 +36,5 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
api 'androidx.constraintlayout:constraintlayout:1.1.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.databinding:viewbinding:8.2.2'
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
import com.kongzue.baseframework.util.swipeback.util.SwipeBackUtil;
import com.kongzue.baseframework.util.toast.Toaster;

import androidx.viewbinding.ViewBinding;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand All @@ -103,7 +105,7 @@
* @link: http://kongzue.com/
* @describe: 自动化代码流水线作业,以及对原生安卓、MIUI、flyme的透明状态栏显示灰色图标文字的支持,同时提供一些小工具简化开发难度,详细说明文档:https://github.com/kongzue/BaseFramework
*/
public abstract class BaseActivity extends AppCompatActivity implements SwipeBackActivityBase {
public abstract class BaseActivity<T extends ViewBinding> extends AppCompatActivity implements SwipeBackActivityBase {

private LifeCircleListener lifeCircleListener; //快速管理生命周期
private static GlobalLifeCircleListener globalLifeCircleListener; //全局生命周期
Expand Down Expand Up @@ -134,6 +136,8 @@ public abstract class BaseActivity extends AppCompatActivity implements SwipeBac
private Bundle savedInstanceState;
private SwipeBackActivityHelper mHelper;

protected T binding;

@Override
@Deprecated
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -165,18 +169,17 @@ protected void onCreate(Bundle savedInstanceState) {
if (layoutResId == -1) {
View contentView = resetContentView();
if (contentView == null) {
if (!isNull(layoutResName)) {
layoutResId = getResources().getIdentifier(layoutResName, "layout", getPackageName());
setContentView(layoutResId);
} else {
layoutResId = getResources().getIdentifier(guessNameOfLayoutResId(), "layout", getPackageName());
if (layoutResId != 0) {
setContentView(layoutResId);
if (BaseFrameworkSettings.useDataBinding) {
View rootView = userDataBindingCreateLayout();
if (rootView != null) {
setContentView(rootView);
} else {
errorLog("请在您的Activity的Class上注解:@Layout(你的layout资源id)或重写resetLayoutResId()方法以设置布局");
return;
guessLayoutResToSetContentView();
}
} else {
guessLayoutResToSetContentView();
}

} else {
setContentView(resetContentView());
}
Expand Down Expand Up @@ -215,6 +218,40 @@ public void run() {
}
}

private void guessLayoutResToSetContentView() {
if (!isNull(layoutResName)) {
layoutResId = getResources().getIdentifier(layoutResName, "layout", getPackageName());
setContentView(layoutResId);
} else {
layoutResId = getResources().getIdentifier(guessNameOfLayoutResId(), "layout", getPackageName());
if (layoutResId != 0) {
setContentView(layoutResId);
} else {
errorLog("请在您的Activity的Class上注解:@Layout(你的layout资源id)或重写resetLayoutResId()方法以设置布局");
}
}
}

private View userDataBindingCreateLayout() {
String className = getClass().getSimpleName();
if (className.endsWith("Activity")) {
className = className.substring(0, className.length() - 8);
}
String bindingClassName = getPackageName() + ".databinding.Activity" + className + "Binding";

try {
// 通过反射实例化Binding对象
Class<?> bindingClass = Class.forName(bindingClassName);
Method inflateMethod = bindingClass.getMethod("inflate", LayoutInflater.class);
binding = (T) inflateMethod.invoke(null, getLayoutInflater());

return binding.getRoot();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

private String guessNameOfLayoutResId() {
String[] words = getClass().getSimpleName().split("(?<!^)(?=[A-Z])");
StringBuffer stringBuffer = new StringBuffer(words.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,7 @@ private static String createDeviceId() {
//全局 Activity 默认退出动画
public static int defaultActivityExitInAnimRes = 0;
public static int defaultActivityExitOutAnimRes = 0;

//使用DataBinding
public static boolean useDataBinding = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -70,6 +71,12 @@ public Permission STORAGE() {
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
return this;
}

public Permission MEDIA() {
permissions.add(Manifest.permission.READ_MEDIA_IMAGES);
permissions.add(Manifest.permission.READ_MEDIA_VIDEO);
return this;
}

public Permission CAMERA() {
permissions.add(Manifest.permission.CAMERA);
Expand Down Expand Up @@ -164,4 +171,9 @@ public Permission RECEIVE_MMS() {
permissions.add(Manifest.permission.RECEIVE_MMS);
return this;
}

public Permission add(String... p) {
permissions.addAll(Arrays.asList(p));
return this;
}
}

0 comments on commit 7c7a5cf

Please sign in to comment.