diff --git a/VirtualApp/app/src/main/java/io/virtualapp/sys/ShareBridgeActivity.java b/VirtualApp/app/src/main/java/io/virtualapp/sys/ShareBridgeActivity.java index f7379402d..270c03089 100644 --- a/VirtualApp/app/src/main/java/io/virtualapp/sys/ShareBridgeActivity.java +++ b/VirtualApp/app/src/main/java/io/virtualapp/sys/ShareBridgeActivity.java @@ -6,6 +6,9 @@ import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; +import android.content.res.Resources; +import android.graphics.drawable.Drawable; +import android.net.Uri; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; @@ -21,6 +24,7 @@ import com.lody.virtual.client.ipc.VActivityManager; import com.lody.virtual.client.ipc.VPackageManager; +import java.util.ArrayList; import java.util.List; import io.virtualapp.R; @@ -30,7 +34,112 @@ */ public class ShareBridgeActivity extends AppCompatActivity { private SharedAdapter mAdapter; - private List mShareComponents; + private List mShareComponents = null; + + private List getCommonComponents(Intent intent) { + Context context = getApplicationContext(); + List mCommonComponents = new ArrayList<>(); + Resources r = getResources(); + ListItem listItem; + listItem = new ListItem( + context.getPackageName(), + InstallerActivity.class.getName(), + r.getString(R.string.app_installer_label), + r.getDrawable(R.mipmap.ic_launcher), + ContextType.APP, + Intent.FLAG_ACTIVITY_NEW_TASK, + intent.getClipData().getItemAt(0).getUri()); + mCommonComponents.add(listItem); + return mCommonComponents; + } + + private enum ContextType { + APP, + VXP + } + + private static class ListItem { + private ContextType contextType; + private PackageManager pm = null; + private String label = null; + private Drawable icon = null; + + private String packageName; + private String activityName; + private ResolveInfo resolveInfo; + + private Uri data; + private Integer flag; + + public String getLabel() { + if (label != null) return label; + if (pm == null) throw new IllegalArgumentException(); + label = String.valueOf(resolveInfo.loadLabel(pm)); + return label; + } + + public Drawable getIcon() { + if (icon != null) return icon; + if (pm == null) throw new IllegalArgumentException(); + icon = resolveInfo.loadIcon(pm); + return icon; + } + + public String getPackageName() { + return packageName; + } + + public String getActivityName() { + return activityName; + } + + public Uri getData() { + return data; + } + + public Integer getFlag() { + return flag; + } + + public ContextType getContextType() { + return contextType; + } + + private ListItem(String packageName, String activityName, String label, Drawable icon, ContextType contextType) { + this.packageName = packageName; + this.activityName = activityName; + this.label = label; + this.icon = icon; + this.contextType = contextType; + } + + private ListItem(String packageName, String activityName, String label, Drawable icon, ContextType contextType, Integer flag, Uri data) { + this(packageName, activityName, label, icon, contextType); + this.flag = flag; + this.data = data; + } + + private ListItem(ResolveInfo resolveInfo, PackageManager pm) { + this(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name, null, null, ContextType.VXP); + this.resolveInfo = resolveInfo; + this.pm = pm; + } + + @Override + public String toString() { + return "ListItem{" + + "pm=" + pm + + ", label='" + label + '\'' + + ", icon=" + icon + + ", packageName='" + packageName + '\'' + + ", activityName='" + activityName + '\'' + + ", resolveInfo=" + resolveInfo + + ", data=" + data + + ", flag=" + flag + + ", contextType=" + contextType + + '}'; + } + } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -48,8 +157,13 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { } try { - mShareComponents = VPackageManager.get(). + List resolveInfoList = VPackageManager.get(). queryIntentActivities(new Intent(Intent.ACTION_SEND), type, 0, 0); // multi-user? + mShareComponents = getCommonComponents(intent); + PackageManager pm = getPackageManager(); + for (ResolveInfo resolveInfo : resolveInfoList) { + mShareComponents.add(new ListItem(resolveInfo, pm)); + } } catch (Throwable ignored) { } @@ -65,10 +179,20 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { mListView.setOnItemClickListener((parent, view, position, id) -> { try { - ResolveInfo item = mAdapter.getItem(position); Intent t = new Intent(intent); - t.setComponent(new ComponentName(item.activityInfo.packageName, item.activityInfo.name)); - VActivityManager.get().startActivity(t, 0); + ListItem item = mAdapter.getItem(position); + t.setComponent(new ComponentName(item.getPackageName(), item.getActivityName())); + if (item.getFlag() != null) { + t.addFlags(item.getFlag()); + } + if (item.getData() != null) { + t.setData(item.getData()); + } + if (item.getContextType() == ContextType.APP) { + getApplicationContext().startActivity(t); + } else if (item.getContextType() == ContextType.VXP) { + VActivityManager.get().startActivity(t, 0); + } } catch (Throwable e) { Toast.makeText(getApplicationContext(), R.string.shared_to_vxp_failed, Toast.LENGTH_SHORT).show(); } @@ -84,7 +208,7 @@ public int getCount() { } @Override - public ResolveInfo getItem(int position) { + public ListItem getItem(int position) { return mShareComponents.get(position); } @@ -104,15 +228,14 @@ public View getView(int position, View convertView, ViewGroup parent) { holder = (ViewHolder) convertView.getTag(); } - ResolveInfo item = getItem(position); - PackageManager packageManager = getPackageManager(); + ListItem item = getItem(position); try { - holder.label.setText(item.loadLabel(packageManager)); + holder.label.setText(item.getLabel()); } catch (Throwable e) { holder.label.setText(R.string.package_state_unknown); } try { - holder.icon.setImageDrawable(item.loadIcon(packageManager)); + holder.icon.setImageDrawable(item.getIcon()); } catch (Throwable e) { holder.icon.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_def_app_icon)); } diff --git a/VirtualApp/lib/src/main/java/com/lody/virtual/helper/utils/FileUtils.java b/VirtualApp/lib/src/main/java/com/lody/virtual/helper/utils/FileUtils.java index 78dd630c2..ed486a1e2 100644 --- a/VirtualApp/lib/src/main/java/com/lody/virtual/helper/utils/FileUtils.java +++ b/VirtualApp/lib/src/main/java/com/lody/virtual/helper/utils/FileUtils.java @@ -159,6 +159,7 @@ public static String getFileFromUri(Context context, Uri packageUri) { File sharedFileCopy = new File(context.getCacheDir(), packageUri.getLastPathSegment()); try { inputStream = context.getContentResolver().openInputStream(packageUri); + sharedFileCopy.getParentFile().mkdirs(); outputStream = new FileOutputStream(sharedFileCopy); byte[] buffer = new byte[1024]; int count;