Skip to content

Commit 10ce022

Browse files
committed
fix(core): 插件的LayoutInflater改为总是从Context取单例
由于ShadowLayoutInflater不再只是一个转调壳子,有了状态(mOriginalFactory等), 不能再总是new新的对象了。换了新对象就无法还原Factory了。 fixup! c710a18 fix #614
1 parent d9fd8cd commit 10ce022

File tree

6 files changed

+95
-8
lines changed

6 files changed

+95
-8
lines changed

projects/sdk/core/loader/src/main/kotlin/com/tencent/shadow/core/loader/delegates/ShadowActivityDelegate.kt

+5-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package com.tencent.shadow.core.loader.delegates
2121
import android.app.Activity
2222
import android.content.ComponentName
2323
import android.content.Context
24-
import android.content.Context.LAYOUT_INFLATER_SERVICE
2524
import android.content.Intent
2625
import android.content.pm.ActivityInfo
2726
import android.content.res.Configuration
@@ -40,7 +39,10 @@ import com.tencent.shadow.core.loader.managers.ComponentManager.Companion.CM_CLA
4039
import com.tencent.shadow.core.loader.managers.ComponentManager.Companion.CM_EXTRAS_BUNDLE_KEY
4140
import com.tencent.shadow.core.loader.managers.ComponentManager.Companion.CM_LOADER_BUNDLE_KEY
4241
import com.tencent.shadow.core.loader.managers.ComponentManager.Companion.CM_PART_KEY
43-
import com.tencent.shadow.core.runtime.*
42+
import com.tencent.shadow.core.runtime.MixResources
43+
import com.tencent.shadow.core.runtime.PluginActivity
44+
import com.tencent.shadow.core.runtime.ShadowActivity
45+
import com.tencent.shadow.core.runtime.ShadowActivityLifecycleCallbacks
4446
import com.tencent.shadow.core.runtime.container.HostActivityDelegate
4547
import com.tencent.shadow.core.runtime.container.HostActivityDelegator
4648

@@ -250,10 +252,7 @@ open class ShadowActivityDelegate(private val mDI: DI) : GeneratedShadowActivity
250252
return mPluginClassLoader
251253
}
252254

253-
override fun getLayoutInflater(): LayoutInflater {
254-
val inflater = mHostActivityDelegator.applicationContext.getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater
255-
return ShadowLayoutInflater.build(inflater, mPluginActivity, mPartKey)
256-
}
255+
override fun getLayoutInflater(): LayoutInflater = LayoutInflater.from(mPluginActivity)
257256

258257
override fun getResources(): Resources {
259258
if (mDependenciesInjected) {

projects/sdk/core/runtime/src/main/java/com/tencent/shadow/core/runtime/PluginActivity.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ public boolean onCreatePanelMenu(int featureId, Menu menu) {
8181
}
8282

8383
public LayoutInflater getLayoutInflater() {
84-
LayoutInflater inflater = hostActivityDelegator.getWindow().getLayoutInflater();
85-
return ShadowLayoutInflater.build(inflater, this, mPartKey);
84+
return LayoutInflater.from(this);
8685
}
8786

8887
//TODO: 对齐原手工代码,这个方法签名实际上不对,应该传入ShadowActivity
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Tencent Shadow available.
3+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
4+
*
5+
* Licensed under the BSD 3-Clause License (the "License"); you may not use
6+
* this file except in compliance with the License. You may obtain a copy of
7+
* the License at
8+
*
9+
* https://opensource.org/licenses/BSD-3-Clause
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package com.tencent.shadow.test.cases.plugin_androidx;
20+
21+
import android.content.Intent;
22+
23+
import androidx.test.core.app.ApplicationProvider;
24+
import androidx.test.ext.junit.runners.AndroidJUnit4;
25+
import androidx.test.filters.LargeTest;
26+
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
30+
31+
@RunWith(AndroidJUnit4.class)
32+
@LargeTest
33+
public class AppCompatActivityTest extends PluginAndroidxAppTest {
34+
35+
@Override
36+
protected Intent getLaunchIntent() {
37+
Intent pluginIntent = new Intent();
38+
String packageName = ApplicationProvider.getApplicationContext().getPackageName();
39+
pluginIntent.setClassName(
40+
packageName,
41+
"com.tencent.shadow.test.plugin.androidx_cases.lib.AppCompatTestActivity"
42+
);
43+
return pluginIntent;
44+
}
45+
46+
@Test
47+
public void testFactory2Class() {
48+
matchTextWithViewTag("factory2Class", "androidx.appcompat.app.AppCompatDelegateImpl");
49+
}
50+
51+
}

projects/test/plugin/androidx-cases/androidx-cases-lib/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ android {
2727

2828
dependencies {
2929
def activity_version = "1.2.2"
30+
def appcompat_version = "1.3.1"
3031

3132
implementation "androidx.activity:activity:$activity_version"
33+
implementation "androidx.appcompat:appcompat:$appcompat_version"
3234
}

projects/test/plugin/androidx-cases/androidx-cases-lib/src/main/AndroidManifest.xml

+3
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@
99

1010
<activity android:name=".LiveDataWithActivityTestActivity" />
1111
<activity android:name=".AppComponentFactoryTestActivity" />
12+
<activity
13+
android:name=".AppCompatTestActivity"
14+
android:theme="@style/Theme.AppCompat" />
1215
</application>
1316
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.tencent.shadow.test.plugin.androidx_cases.lib;
2+
3+
import android.os.Bundle;
4+
import android.view.LayoutInflater;
5+
import android.view.ViewGroup;
6+
7+
import androidx.annotation.Nullable;
8+
import androidx.appcompat.app.AppCompatActivity;
9+
10+
public class AppCompatTestActivity extends AppCompatActivity {
11+
@Override
12+
protected void onCreate(@Nullable Bundle savedInstanceState) {
13+
super.onCreate(savedInstanceState);
14+
15+
LayoutInflater.Factory2 factory2 = getLayoutInflater().getFactory2();
16+
String factory2Class;
17+
if (factory2 == null) {
18+
factory2Class = "null";
19+
} else {
20+
factory2Class = factory2.getClass().getName();
21+
}
22+
23+
ViewGroup viewGroup = UiUtil.setActivityContentView(this);
24+
ViewGroup item = UiUtil.makeItem(
25+
this,
26+
"factory2Class",
27+
"factory2Class",
28+
factory2Class
29+
);
30+
31+
viewGroup.addView(item);
32+
}
33+
}

0 commit comments

Comments
 (0)