Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix FileNotFoundException by mkdir before save file; add apk installer to send receiver list #1055

Open
wants to merge 5 commits into
base: vxp
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 133 additions & 10 deletions VirtualApp/app/src/main/java/io/virtualapp/sys/ShareBridgeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -30,7 +34,112 @@
*/
public class ShareBridgeActivity extends AppCompatActivity {
private SharedAdapter mAdapter;
private List<ResolveInfo> mShareComponents;
private List<ListItem> mShareComponents = null;

private List<ListItem> getCommonComponents(Intent intent) {
Context context = getApplicationContext();
List<ListItem> 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) {
Expand All @@ -48,8 +157,13 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
}

try {
mShareComponents = VPackageManager.get().
List<ResolveInfo> 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) {
}

Expand All @@ -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();
}
Expand All @@ -84,7 +208,7 @@ public int getCount() {
}

@Override
public ResolveInfo getItem(int position) {
public ListItem getItem(int position) {
return mShareComponents.get(position);
}

Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down