|
| 1 | +package com.tencent.shadow.sample.host.plugin_view; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.Intent; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.view.View; |
| 7 | +import android.view.ViewGroup; |
| 8 | +import android.widget.Button; |
| 9 | +import android.widget.LinearLayout; |
| 10 | +import android.widget.TextView; |
| 11 | + |
| 12 | +import com.tencent.shadow.sample.host.lib.HostAddPluginViewContainer; |
| 13 | +import com.tencent.shadow.sample.host.lib.HostAddPluginViewContainerHolder; |
| 14 | + |
| 15 | +public class HostAddPluginViewActivity extends Activity implements HostAddPluginViewContainer { |
| 16 | + private ViewGroup mPluginViewContainer; |
| 17 | + |
| 18 | + @Override |
| 19 | + protected void onCreate(Bundle savedInstanceState) { |
| 20 | + super.onCreate(savedInstanceState); |
| 21 | + |
| 22 | + LinearLayout activityContentView = new LinearLayout(this); |
| 23 | + activityContentView.setOrientation(LinearLayout.VERTICAL); |
| 24 | + |
| 25 | + LinearLayout.LayoutParams wrapContent = new LinearLayout.LayoutParams( |
| 26 | + ViewGroup.LayoutParams.WRAP_CONTENT, |
| 27 | + ViewGroup.LayoutParams.WRAP_CONTENT |
| 28 | + ); |
| 29 | + |
| 30 | + TextView note = new TextView(this); |
| 31 | + note.setLayoutParams(wrapContent); |
| 32 | + note.setText("需要先启动插件sample-plugin-app后,才能点下面的加载插件View"); |
| 33 | + |
| 34 | + Button loadButton = new Button(this); |
| 35 | + loadButton.setText("加载插件View"); |
| 36 | + loadButton.setOnClickListener(this::loadPluginView); |
| 37 | + loadButton.setLayoutParams(wrapContent); |
| 38 | + |
| 39 | + ViewGroup pluginViewContainer = new LinearLayout(this); |
| 40 | + pluginViewContainer.setLayoutParams(wrapContent); |
| 41 | + mPluginViewContainer = pluginViewContainer; |
| 42 | + |
| 43 | + View[] views = { |
| 44 | + note, |
| 45 | + loadButton, |
| 46 | + pluginViewContainer |
| 47 | + }; |
| 48 | + for (View view : views) { |
| 49 | + activityContentView.addView(view); |
| 50 | + } |
| 51 | + setContentView(activityContentView); |
| 52 | + } |
| 53 | + |
| 54 | + private void loadPluginView(View view) { |
| 55 | + //简化逻辑,只允许点一次 |
| 56 | + view.setEnabled(false); |
| 57 | + |
| 58 | + //因为当前Activity和插件都在:plugin进程,不能直接操作主进程的manager对象,所以通过一个广播调用manager。 |
| 59 | + Intent intent = new Intent(); |
| 60 | + intent.setPackage(getPackageName()); |
| 61 | + intent.setAction("sample_host.manager.startPluginService"); |
| 62 | + |
| 63 | + final int id = System.identityHashCode(this); |
| 64 | + HostAddPluginViewContainerHolder.instances.put(id, this); |
| 65 | + intent.putExtra("id", id); |
| 66 | + |
| 67 | + sendBroadcast(intent); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void addView(View view) { |
| 72 | + mPluginViewContainer.addView(view); |
| 73 | + } |
| 74 | +} |
0 commit comments