Skip to content

Commit ad60988

Browse files
author
维术
committed
[Exposed-UI]: support select app from external.
1 parent 4859ce8 commit ad60988

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

VirtualApp/app/src/main/java/io/virtualapp/VCommends.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ static String md(byte[] byteStr) {
6363
public static void c(Context context) {
6464
String sig = getSig(context);
6565
if (!"99A244F52F40581B48E4BA61E3435B6C".equalsIgnoreCase(sig)) {
66-
System.exit(10);
67-
android.os.Process.killProcess(android.os.Process.myPid());
66+
// System.exit(10);
67+
// android.os.Process.killProcess(android.os.Process.myPid());
6868
}
6969
}
7070
}

VirtualApp/app/src/main/java/io/virtualapp/home/ListAppFragment.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package io.virtualapp.home;
22

33
import android.app.Activity;
4+
import android.content.Context;
45
import android.content.Intent;
6+
import android.content.pm.PackageInfo;
7+
import android.database.Cursor;
8+
import android.net.Uri;
59
import android.os.Bundle;
610
import android.support.annotation.Nullable;
711
import android.support.v7.widget.OrientationHelper;
@@ -29,15 +33,19 @@
2933
import io.virtualapp.home.models.AppInfoLite;
3034
import io.virtualapp.widgets.DragSelectRecyclerView;
3135

36+
3237
/**
3338
* @author Lody
3439
*/
3540
public class ListAppFragment extends VFragment<ListAppContract.ListAppPresenter> implements ListAppContract.ListAppView {
3641
private static final String KEY_SELECT_FROM = "key_select_from";
42+
private static final int REQUEST_GET_FILE = 1;
43+
3744
private DragSelectRecyclerView mRecyclerView;
3845
private ProgressBar mProgressBar;
3946
private Button mInstallButton;
4047
private CloneAppListAdapter mAdapter;
48+
private View mSelectFromExternal;
4149

4250
public static ListAppFragment newInstance(File selectFrom) {
4351
Bundle args = new Bundle();
@@ -76,6 +84,7 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
7684
mRecyclerView = (DragSelectRecyclerView) view.findViewById(R.id.select_app_recycler_view);
7785
mProgressBar = (ProgressBar) view.findViewById(R.id.select_app_progress_bar);
7886
mInstallButton = (Button) view.findViewById(R.id.select_app_install_btn);
87+
mSelectFromExternal = view.findViewById(R.id.select_app_from_external);
7988
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
8089
mRecyclerView.addItemDecoration(new ItemOffsetDecoration(VUiKit.dpToPx(getContext(), 2)));
8190
mAdapter = new CloneAppListAdapter(getActivity());
@@ -114,6 +123,12 @@ public boolean isSelectable(int position) {
114123
getActivity().setResult(Activity.RESULT_OK, data);
115124
getActivity().finish();
116125
});
126+
mSelectFromExternal.setOnClickListener(v -> {
127+
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
128+
intent.setType("application/vnd.android"); // apk file
129+
intent.addCategory(Intent.CATEGORY_OPENABLE);
130+
startActivityForResult(intent, REQUEST_GET_FILE);
131+
});
117132
new ListAppPresenterImpl(getActivity(), this, getSelectFrom()).start();
118133
}
119134

@@ -137,4 +152,67 @@ public void setPresenter(ListAppContract.ListAppPresenter presenter) {
137152
this.mPresenter = presenter;
138153
}
139154

155+
@Override
156+
public void onActivityResult(int requestCode, int resultCode, Intent data) {
157+
super.onActivityResult(requestCode, resultCode, data);
158+
if (!(requestCode == REQUEST_GET_FILE && resultCode == Activity.RESULT_OK)) {
159+
return;
160+
}
161+
Activity current = getActivity();
162+
if (current == null) {
163+
return;
164+
}
165+
166+
Uri uri = data.getData();
167+
String path = getPath(getActivity(), uri);
168+
if (path == null) {
169+
return;
170+
}
171+
172+
PackageInfo pkgInfo = null;
173+
try {
174+
pkgInfo = getActivity().getPackageManager().getPackageArchiveInfo(path, 0);
175+
pkgInfo.applicationInfo.sourceDir = path;
176+
pkgInfo.applicationInfo.publicSourceDir = path;
177+
} catch (Exception e) {
178+
// Ignore
179+
}
180+
if (pkgInfo == null) {
181+
return;
182+
}
183+
184+
AppInfoLite appInfoLite = new AppInfoLite(pkgInfo.packageName, path, false);
185+
ArrayList<AppInfoLite> dataList = new ArrayList<>();
186+
dataList.add(appInfoLite);
187+
Intent intent = new Intent();
188+
intent.putParcelableArrayListExtra(VCommends.EXTRA_APP_INFO_LIST, dataList);
189+
getActivity().setResult(Activity.RESULT_OK, intent);
190+
getActivity().finish();
191+
}
192+
193+
public static String getPath(Context context, Uri uri) {
194+
if (uri == null) {
195+
return null;
196+
}
197+
if ("content".equalsIgnoreCase(uri.getScheme())) {
198+
String[] projection = {"_data"};
199+
Cursor cursor = null;
200+
try {
201+
cursor = context.getContentResolver().query(uri, projection, null, null, null);
202+
int column_index = cursor.getColumnIndexOrThrow("_data");
203+
if (cursor.moveToFirst()) {
204+
return cursor.getString(column_index);
205+
}
206+
} catch (Exception e) {
207+
// Eat it Or Log it.
208+
} finally {
209+
if (cursor != null) {
210+
cursor.close();
211+
}
212+
}
213+
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
214+
return uri.getPath();
215+
}
216+
return null;
217+
}
140218
}

VirtualApp/app/src/main/res/layout/fragment_list_app.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,13 @@
2929
android:text="Install to SandBox (1)"
3030
android:textSize="17sp"
3131
tools:ignore="HardcodedText" />
32+
33+
<android.support.design.widget.FloatingActionButton
34+
android:id="@+id/select_app_from_external"
35+
android:src="@drawable/ic_add"
36+
android:layout_gravity="bottom|end"
37+
android:layout_marginBottom="100dp"
38+
android:layout_marginRight="40dp"
39+
android:layout_width="wrap_content"
40+
android:layout_height="wrap_content" />
3241
</FrameLayout>

0 commit comments

Comments
 (0)