11package io .virtualapp .home ;
22
33import android .app .Activity ;
4+ import android .content .Context ;
45import android .content .Intent ;
6+ import android .content .pm .PackageInfo ;
7+ import android .database .Cursor ;
8+ import android .net .Uri ;
59import android .os .Bundle ;
610import android .support .annotation .Nullable ;
711import android .support .v7 .widget .OrientationHelper ;
2933import io .virtualapp .home .models .AppInfoLite ;
3034import io .virtualapp .widgets .DragSelectRecyclerView ;
3135
36+
3237/**
3338 * @author Lody
3439 */
3540public 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}
0 commit comments