cc4896b171cda82b15f58242564bdf5df733b825
[iec.git] / src / type3_AndroidCloud / anbox-master / android / appmgr / src / org / anbox / appmgr / AppModel.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.Context;
27 import android.content.pm.ApplicationInfo;
28 import android.graphics.drawable.Drawable;
29
30 import java.io.File;
31
32 /**
33  * @credit http://developer.android.com/reference/android/content/AsyncTaskLoader.html
34  */
35 public class AppModel {
36
37     private final Context mContext;
38     private final ApplicationInfo mInfo;
39
40     private String mAppLabel;
41     private Drawable mIcon;
42
43     private boolean mMounted;
44     private final File mApkFile;
45
46     public AppModel(Context context, ApplicationInfo info) {
47         mContext = context;
48         mInfo = info;
49
50         mApkFile = new File(info.sourceDir);
51     }
52
53     public ApplicationInfo getAppInfo() {
54         return mInfo;
55     }
56
57     public String getApplicationPackageName() {
58         return getAppInfo().packageName;
59     }
60
61     public String getLabel() {
62         return mAppLabel;
63     }
64
65     public Drawable getIcon() {
66         if (mIcon == null) {
67             if (mApkFile.exists()) {
68                 mIcon = mInfo.loadIcon(mContext.getPackageManager());
69                 return mIcon;
70             } else {
71                 mMounted = false;
72             }
73         } else if (!mMounted) {
74             // If the app wasn't mounted but is now mounted, reload
75             // its icon.
76             if (mApkFile.exists()) {
77                 mMounted = true;
78                 mIcon = mInfo.loadIcon(mContext.getPackageManager());
79                 return mIcon;
80             }
81         } else {
82             return mIcon;
83         }
84
85         return mContext.getResources().getDrawable(android.R.drawable.sym_def_app_icon);
86     }
87
88
89     void loadLabel(Context context) {
90         if (mAppLabel == null || !mMounted) {
91             if (!mApkFile.exists()) {
92                 mMounted = false;
93                 mAppLabel = mInfo.packageName;
94             } else {
95                 mMounted = true;
96                 CharSequence label = mInfo.loadLabel(context.getPackageManager());
97                 mAppLabel = label != null ? label.toString() : mInfo.packageName;
98             }
99         }
100     }
101 }