文章目录
一、启动应用源码分析
1、HomeActivity 启动应用点击方法
2、HomePresenterImpl 启动应用方法
3、VirtualCore 启动插件应用最终方法
一、启动应用源码分析
1、HomeActivity 启动应用点击方法
在 io.virtualapp.home.HomeActivity 页面中 , 点击列表中的应用后 , 可以启动应用 ;
直接到 io.virtualapp.home.adapters.LaunchpadAdapter 适配器中查找点击事件源码 , 调用的是 private OnAppClickListener mAppClickListener; 成员变量的 onAppClick 方法 ;
该成员变量是通过 public void setAppClickListener(OnAppClickListener mAppClickListener) 方法注入的 ;
public class LaunchpadAdapter extends RecyclerView.Adapter<LaunchpadAdapter.ViewHolder> { private LayoutInflater mInflater; private List<AppData> mList; private SparseIntArray mColorArray = new SparseIntArray(); private OnAppClickListener mAppClickListener; public void setAppClickListener(OnAppClickListener mAppClickListener) { this.mAppClickListener = mAppClickListener; } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.itemView.setBackgroundColor(holder.color); holder.itemView.setOnClickListener(v -> { if (mAppClickListener != null) { mAppClickListener.onAppClick(position, data); } }); } }
2、HomePresenterImpl 启动应用方法
调用 LaunchpadAdapter.setAppClickListener 方法注入应用启动点击事件 ,
public class HomeActivity extends VActivity implements HomeContract.HomeView { private static final String TAG = HomeActivity.class.getSimpleName(); private HomeContract.HomePresenter mPresenter; private LaunchpadAdapter mLaunchpadAdapter; private void initLaunchpad() { mLaunchpadAdapter.setAppClickListener((pos, data) -> { if (!data.isLoading()) { if (data instanceof AddAppButton) { onAddAppButtonClick(); } mLaunchpadAdapter.notifyItemChanged(pos); mPresenter.launchApp(data); } }); } }
应用启动 , 实际上调用的是 io.virtualapp.home.HomePresenterImpl 的 public void launchApp(AppData data) 方法 ;
class HomePresenterImpl implements HomeContract.HomePresenter { @Override public void launchApp(AppData data) { try { if (data instanceof PackageAppData) { PackageAppData appData = (PackageAppData) data; Log.i("HSL", "PackageAppData : " + appData.toString()); appData.isFirstOpen = false; LoadingActivity.launch(mActivity, appData.packageName, 0); } else if (data instanceof MultiplePackageAppData) { MultiplePackageAppData multipleData = (MultiplePackageAppData) data; Log.i("HSL", "MultiplePackageAppData : " + multipleData.toString()); multipleData.isFirstOpen = false; LoadingActivity.launch(mActivity, multipleData.appInfo.packageName, ((MultiplePackageAppData) data).userId); } } catch (Throwable e) { e.printStackTrace(); } } }
拦截一下数据 , 上述两个位置打印出来的日志如下 :
I/HSL: PackageAppData : PackageAppData{packageName='kim.hsl.svg', name='SVG', icon=android.graphics.drawable.BitmapDrawable@8d312bf, fastOpen=false, isFirstOpen=false, isLoading=false}
3、VirtualCore 启动插件应用最终方法
最终启动应用的方法是 io.virtualapp.home.LoadingActivity 类中的 public static void launch(Context context, String packageName, int userId) 方法 ;
先从 VirtualCore 中获取启动的 Intent 1 , 然后启动一个 VActivity , 创建 intent 2 , 将 从 VirtualCore 中获取启动的 Intent 放到 intent 2 中 ;
public class LoadingActivity extends VActivity { private static final String PKG_NAME_ARGUMENT = "MODEL_ARGUMENT"; private static final String KEY_INTENT = "KEY_INTENT"; private static final String KEY_USER = "KEY_USER"; private PackageAppData appModel; public static void launch(Context context, String packageName, int userId) { Intent intent = VirtualCore.get().getLaunchIntent(packageName, userId); Log.i("HSL", "packageName : " + packageName + " , userId : " + userId); if (intent != null) { Intent loadingPageIntent = new Intent(context, LoadingActivity.class); loadingPageIntent.putExtra(PKG_NAME_ARGUMENT, packageName); loadingPageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); loadingPageIntent.putExtra(KEY_INTENT, intent); loadingPageIntent.putExtra(KEY_USER, userId); context.startActivity(loadingPageIntent); } } }
打印的数据 :
I/HSL: packageName : kim.hsl.svg , userId : 0