Skip to content

Commit

Permalink
增加对android q的适配,绕过mFactorySet的调用(暂不发布)
Browse files Browse the repository at this point in the history
  • Loading branch information
JavaNoober committed Aug 19, 2019
1 parent cb9811a commit 350a1d6
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 112 deletions.
8 changes: 4 additions & 4 deletions androidx/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 28
compileSdkVersion 29


defaultConfig {
minSdkVersion 16
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"

Expand Down Expand Up @@ -40,8 +40,8 @@ dependencies {

// If you want pre-written Activities and Fragments you can subclass as providers
implementation 'com.trello.rxlifecycle3:rxlifecycle-components:3.0.0'
// implementation project(':libraryx')
implementation 'com.noober.background:corex:1.5.4-ALPHA'
implementation project(':libraryx')
// implementation 'com.noober.background:corex:1.5.4-ALPHA'
}

repositories{
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.noober.background:core:1.5.4'
// implementation project(':library')
// implementation 'com.noober.background:core:1.5.4'
implementation project(':library')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
setContentView( R.layout.activity_main);
getSupportFragmentManager().beginTransaction().add(R.id.fl_content, new BlankFragment()).commitAllowingStateLoss();
Button button = findViewById(R.id.btn);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down
72 changes: 36 additions & 36 deletions app/src/main/java/com/noober/backgroudlibrary/MyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,41 @@ public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
BackgroundLibrary.inject(activity);
}

@Override
public void onActivityStarted(Activity activity) {

}

@Override
public void onActivityResumed(Activity activity) {

}

@Override
public void onActivityPaused(Activity activity) {

}

@Override
public void onActivityStopped(Activity activity) {

}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

}

@Override
public void onActivityDestroyed(Activity activity) {

}
});
// registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
// @Override
// public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
// BackgroundLibrary.inject(activity);
// }
//
// @Override
// public void onActivityStarted(Activity activity) {
//
// }
//
// @Override
// public void onActivityResumed(Activity activity) {
//
// }
//
// @Override
// public void onActivityPaused(Activity activity) {
//
// }
//
// @Override
// public void onActivityStopped(Activity activity) {
//
// }
//
// @Override
// public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
//
// }
//
// @Override
// public void onActivityDestroyed(Activity activity) {
//
// }
// });
}
}
21 changes: 13 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
android:gravity="center"
android:text="指定只显示左右下方的边框"
android:textSize="20sp"
app:bl_stroke_width="1dp"
app:bl_stroke_color="@android:color/black"
app:bl_solid_color="@android:color/white"
app:bl_stroke_position="left|bottom|right"/>
app:bl_stroke_color="@android:color/black"
app:bl_stroke_position="left|bottom|right"
app:bl_stroke_width="1dp" />

<com.noober.background.view.BLTextView
android:id="@+id/ttt"
Expand All @@ -45,9 +45,9 @@

<com.noober.background.view.BLView
android:id="@+id/v_anim"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:bl_anim_auto_start="true"
app:bl_duration="50"
app:bl_frame_drawable_item0="@drawable/img00"
Expand Down Expand Up @@ -108,11 +108,11 @@
android:textSize="18dp"
android:textStyle="bold"
app:bl_corners_radius="3dp"
app:bl_shape="rectangle"
app:bl_stroke_width="1dp"
app:bl_multi_selector1="-state_enabled,#9DD1F6"
app:bl_multi_selector2="state_pressed,#9DD1F6"
app:bl_multi_selector3="-state_pressed,#1B82D1"/>
app:bl_multi_selector3="-state_pressed,#1B82D1"
app:bl_shape="rectangle"
app:bl_stroke_width="1dp" />

<com.noober.background.view.BLButton
android:layout_width="300dp"
Expand All @@ -126,9 +126,9 @@
android:textSize="18dp"
android:textStyle="bold"
app:bl_corners_radius="3dp"
app:bl_pressed_drawable="#9DD1F6"
app:bl_shape="rectangle"
app:bl_stroke_width="1dp"
app:bl_pressed_drawable="#9DD1F6"
app:bl_unEnabled_drawable="#9DD1F6"
app:bl_unPressed_drawable="#1B82D1" />

Expand Down Expand Up @@ -494,5 +494,10 @@

</FrameLayout>

<!--<fragment-->
<!--android:id="@+id/fragmentTest"-->
<!--android:name="com.noober.backgroudlibrary.BlankFragment"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent" />-->
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
105 changes: 74 additions & 31 deletions library/src/main/java/com/noober/background/BackgroundLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.LayoutInflaterCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* minSdkVersion最小为14,建议minSdkVersion >= 16
* 如果minSdkVersion < 16:bl_gradient_angle, bl_gradient_startColor, bl_gradient_centerColor, bl_gradient_endColor会失效,其他正常
*
* <p>
* Created by xiaoqi on 2018/9/9
*/
public class BackgroundLibrary {
Expand All @@ -25,30 +29,37 @@ public static LayoutInflater inject(Context context) {
} else {
inflater = LayoutInflater.from(context);
}
if(inflater == null){
if (inflater == null) {
return null;
}
if(inflater.getFactory2() == null){
BackgroundFactory factory = new BackgroundFactory();
if (context instanceof AppCompatActivity) {
final AppCompatDelegate delegate = ((AppCompatActivity) context).getDelegate();
factory.setInterceptFactory(new LayoutInflater.Factory() {
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
return delegate.createView(null, name, context, attrs);
}
});
}
if (inflater.getFactory2() == null) {
BackgroundFactory factory = setDelegateFactory(context);
inflater.setFactory2(factory);
}else if(!(inflater.getFactory2() instanceof BackgroundFactory)){
forceSetFactory2(inflater);
} else if (!(inflater.getFactory2() instanceof BackgroundFactory)) {
forceSetFactory2(inflater, context);
}
return inflater;
}

@NonNull
private static BackgroundFactory setDelegateFactory(Context context) {
BackgroundFactory factory = new BackgroundFactory();
if (context instanceof AppCompatActivity) {
final AppCompatDelegate delegate = ((AppCompatActivity) context).getDelegate();
factory.setInterceptFactory(new LayoutInflater.Factory() {
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
return delegate.createView(null, name, context, attrs);
}
});
}
return factory;
}

/**
* used for activity which has addFactory
* 如果因为其他库已经设置了factory,可以使用该方法去进行inject,在其他库的setFactory后面调用即可
*
* @param context
*/
public static LayoutInflater inject2(Context context) {
Expand All @@ -58,32 +69,64 @@ public static LayoutInflater inject2(Context context) {
} else {
inflater = LayoutInflater.from(context);
}
if(inflater == null){
if (inflater == null) {
return null;
}
forceSetFactory2(inflater);
forceSetFactory2(inflater, context);
return inflater;
}

private static void forceSetFactory2(LayoutInflater inflater) {
/**
* 通过LayoutInflaterCompat去规避非SDK接口在Android Q 中的受限。
* 首先:设置LayoutInflaterCompat的sCheckedField为false, 保证可以设置当前mFactory2的值
* 第二:设置LayoutInflater的 mFactory 为空,保证LayoutInflaterCompat调用setFactory的时候不进行FactoryMerger操作
* 第三:反射调用LayoutInflaterCompat的forceSetFactory2方法
* 第四:重新设置LayoutInflater的mFactory值,防止调用Fragment的时候fragment会进行FactoryMerger操作
* 经过上述步骤,在Activity以及Activity中的Fragment就会变成我们想要的factory类
*/
private static void forceSetFactory2(LayoutInflater inflater, Context context) {
Class<LayoutInflaterCompat> compatClass = LayoutInflaterCompat.class;
Class<LayoutInflater> inflaterClass = LayoutInflater.class;
try {
Field field = LayoutInflater.class.getDeclaredField("mFactorySet");
field.setAccessible(true);
field.setBoolean(inflater, false);
Field sCheckedField = compatClass.getDeclaredField("sCheckedField");
sCheckedField.setAccessible(true);
sCheckedField.setBoolean(inflater, false);
Field mFactory = inflaterClass.getDeclaredField("mFactory");
mFactory.setAccessible(true);
mFactory.set(inflater, null);

BackgroundFactory factory = new BackgroundFactory();
if (inflater.getFactory2() != null) {
factory.setInterceptFactory2(inflater.getFactory2());
} else if (inflater.getFactory() != null) {
factory.setInterceptFactory(inflater.getFactory());
}
inflater.setFactory2(factory);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
Method method = compatClass.getDeclaredMethod("forceSetFactory2", LayoutInflater.class, LayoutInflater.Factory2.class);
method.setAccessible(true);
BackgroundFactory factory = setDelegateFactory(context);
method.invoke(null, inflater, factory);
mFactory.set(inflater, factory);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
// try {
// Field field = LayoutInflater.class.getDeclaredField("mFactorySet");
// field.setAccessible(true);
// field.setBoolean(inflater, false);
//
// BackgroundFactory factory = new BackgroundFactory();
// if (inflater.getFactory2() != null) {
// factory.setInterceptFactory2(inflater.getFactory2());
// } else if (inflater.getFactory() != null) {
// factory.setInterceptFactory(inflater.getFactory());
// }
// inflater.setFactory2(factory);
// } catch (NoSuchFieldException e) {
// e.printStackTrace();
// } catch (IllegalArgumentException e) {
// e.printStackTrace();
// } catch (IllegalAccessException e) {
// e.printStackTrace();
// }
}
}
Loading

0 comments on commit 350a1d6

Please sign in to comment.