TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / android / appmgr / src / org / anbox / appmgr / AppsGridFragment.java
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright 2016 Arnab Chakraborty. http://arnab.ch
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of this
7  * software and associated documentation files (the "Software"), to deal in the Software
8  * without restriction, including without limitation the rights to use, copy, modify,
9  * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to the following
11  * conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies
14  * or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
17  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
18  * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23
24 package org.anbox.appmgr;
25
26 import android.content.Intent;
27 import android.os.Bundle;
28 import android.support.v4.app.LoaderManager;
29 import android.support.v4.content.Loader;
30 import android.view.View;
31 import android.widget.GridView;
32
33 import java.util.ArrayList;
34
35 /**
36  * Created by Arnab Chakraborty
37  */
38 public class AppsGridFragment extends GridFragment implements LoaderManager.LoaderCallbacks<ArrayList<AppModel>> {
39
40     AppListAdapter mAdapter;
41
42     @Override
43     public void onActivityCreated(Bundle savedInstanceState) {
44         super.onActivityCreated(savedInstanceState);
45
46         setEmptyText("No Applications");
47
48         mAdapter = new AppListAdapter(getActivity());
49         setGridAdapter(mAdapter);
50
51         // till the data is loaded display a spinner
52         setGridShown(false);
53
54         // create the loader to load the apps list in background
55         getLoaderManager().initLoader(0, null, this);
56     }
57
58     @Override
59     public Loader<ArrayList<AppModel>> onCreateLoader(int id, Bundle bundle) {
60         return new AppsLoader(getActivity());
61     }
62
63     @Override
64     public void onLoadFinished(Loader<ArrayList<AppModel>> loader, ArrayList<AppModel> apps) {
65         mAdapter.setData(apps);
66
67         if (isResumed()) {
68             setGridShown(true);
69         } else {
70             setGridShownNoAnimation(true);
71         }
72     }
73
74     @Override
75     public void onLoaderReset(Loader<ArrayList<AppModel>> loader) {
76         mAdapter.setData(null);
77     }
78
79     @Override
80     public void onGridItemClick(GridView g, View v, int position, long id) {
81         AppModel app = (AppModel) getGridAdapter().getItem(position);
82         if (app != null) {
83             Intent intent = getActivity().getPackageManager().getLaunchIntentForPackage(app.getApplicationPackageName());
84
85             if (intent != null) {
86                 startActivity(intent);
87             }
88         }
89     }
90 }