Release.
[mobilnebezpieczenstwo.git] / src / com / samsung / srpol / loader / AppDetails.java
1 /*
2    Copyright (C) 2014  Samsung Electronics Polska Sp. z o.o.
3
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU AFFERO General Public License as published by
6     the Free Software Foundation, either version 3 of the License, or
7     (at your option) any later version.
8     You may obtain a copy of the License at
9
10                 http://www.gnu.org/licenses/agpl-3.0.txt
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 package com.samsung.srpol.loader;
22
23 import java.io.File;
24 import java.util.Comparator;
25
26 import android.content.pm.ApplicationInfo;
27 import android.content.pm.PackageInfo;
28 import android.content.pm.PackageManager.NameNotFoundException;
29 import android.graphics.ColorMatrix;
30 import android.graphics.ColorMatrixColorFilter;
31 import android.graphics.drawable.Drawable;
32 import android.os.Build;
33
34 /**
35  * Helper class containing details about given permission
36  * 
37  * This Class contains : String mPermissionName; String
38  *         mPermissionLabel; String mPermissionDetails;
39  */
40 public class AppDetails {
41     public static final String TAG = "AppDetails";
42     private static final ColorMatrixColorFilter mGrayscaleFilter;
43
44     private String mAppName;
45     private String mPackageName;
46     private Drawable mAppIcon;
47     private boolean mSystemApp;
48
49     private int mSubCategoriesMask;
50
51     private static AppListLoader mLoader;
52     private boolean mEnabled;
53     private boolean mMounted;
54     private final File mApkFile;
55
56     static {
57         ColorMatrix matrix = new ColorMatrix();
58         matrix.setSaturation(0);
59         mGrayscaleFilter = new ColorMatrixColorFilter(matrix);
60     }
61
62     public AppDetails(AppListLoader loader, PackageInfo packageinfo) {
63         mLoader = loader;
64         mPackageName = packageinfo.applicationInfo.packageName;
65         mEnabled = packageinfo.applicationInfo.enabled;
66         mApkFile = new File(packageinfo.applicationInfo.sourceDir);
67         if (!mApkFile.exists()) {
68             mMounted = false;
69             mAppName = packageinfo.applicationInfo.packageName;
70         } else {
71             mMounted = true;
72             CharSequence label = packageinfo.applicationInfo.loadLabel(mLoader.getPm());
73             mAppName = label != null ? label.toString()
74                     : packageinfo.applicationInfo.packageName;
75         }
76         mSystemApp = (packageinfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0;
77     }
78
79     /**
80      * @return the mAppName
81      */
82     public String getAppName() {
83         return mAppName;
84     }
85
86     /**
87      * @return the mAppPackageName
88      */
89     public String getAppPackageName() {
90         return mPackageName;
91     }
92
93     /**
94      * @return the mAppIcon
95      */
96     public Drawable getAppIcon() {
97         if (mAppIcon == null || !mMounted) {
98             if (mApkFile.exists()) {
99                 try {
100                     mAppIcon = mLoader.getPm().getApplicationIcon(mPackageName);
101                 } catch (NameNotFoundException e) {
102                     mAppIcon = mLoader.getContext().getResources()
103                             .getDrawable(android.R.drawable.sym_def_app_icon);
104                 }
105                 mMounted = true;
106                 return updateAppIconColor(mAppIcon);
107             } else {
108                 mMounted = false;
109                 return updateAppIconColor(mLoader.getContext().getResources()
110                         .getDrawable(android.R.drawable.sym_def_app_icon));
111             }
112         } else
113             return mAppIcon;
114     }
115
116     public int getSubcategoriesMask() {
117         return mSubCategoriesMask;
118     }
119
120     public boolean isInSubcategory(int subcategoryId) {
121         return (mSubCategoriesMask & subcategoryId) > 0;
122     }
123     
124     public boolean isInAllSubcategories(int subcategoryIds) {
125         return (mSubCategoriesMask & subcategoryIds) == subcategoryIds;
126     }
127
128     public boolean isEnabled() {
129         return mEnabled;
130     }
131
132     public void setEnabled(boolean state) {
133         mEnabled = state;
134         updateAppIconColor(mAppIcon);
135     }
136
137     private Drawable updateAppIconColor(Drawable icon) {
138         // disabling and enabling apps in system application manager is
139         // available since API 4.0
140         if (icon != null
141                 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
142             if (mEnabled) {
143                 icon.setColorFilter(null);
144             } else {
145                 icon.setColorFilter(mGrayscaleFilter);
146             }
147         }
148         return icon;
149     }
150
151     /**
152      * @return the mSystemApp
153      */
154     public boolean isSystemApp() {
155         return mSystemApp;
156     }
157
158     public void addSubcategory(int id) {
159         mSubCategoriesMask |= id;
160     }
161
162     /**
163      * @return the mAppPackageName
164      */
165     @Override
166     public String toString() {
167         return mPackageName;
168     }
169
170     @Override
171     public boolean equals(Object o) {
172         if (o instanceof AppDetails) {
173             AppDetails appDetails = (AppDetails) o;
174             if (appDetails.getAppPackageName().equals(mPackageName)) {
175                 return true;
176             }
177         }
178         return false;
179     }
180
181     /**
182      * Perform inteligent comparison of application entry objects.
183      */
184     public static final Comparator<AppDetails> SMART_COMPARATOR = new Comparator<AppDetails>() {
185         @Override
186         public int compare(AppDetails object1, AppDetails object2) {
187             if (object1.mSystemApp) {
188                 if (object2.mSystemApp)
189                     return object1.mAppName.compareTo(object2.mAppName);
190                 return 1;
191             } else {
192                 if (object2.mSystemApp)
193                     return -1;
194                 return object1.mAppName.compareTo(object2.mAppName);
195             }
196         }
197     };
198 }