Initial commit.
[mobilnebezpieczenstwo.git] / src / com / samsung / srpol / utils / Utils.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.utils;
22
23 import com.samsung.srpol.R;
24
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.SharedPreferences;
28 import android.content.pm.PackageManager.NameNotFoundException;
29 import android.graphics.drawable.Drawable;
30 import android.net.Uri;
31 import android.os.Build;
32 import android.provider.Settings;
33
34 public class Utils {
35
36     private static final String SCHEME = "package";
37
38     private static final String APP_PKG_NAME_21 = "com.android.settings.ApplicationPkgName";
39
40     private static final String APP_PKG_NAME_22 = "pkg";
41
42     private static final String APP_DETAILS_PACKAGE_NAME = "com.android.settings";
43
44     private static final String APP_DETAILS_CLASS_NAME = "com.android.settings.InstalledAppDetails";
45
46     private static final String MY_PREFERENCES = "my_preferences";
47
48     private static Drawable mSystemIcon;
49     private static Drawable mSystemIconDisable;
50
51     /**
52      * @return the mSystemIcon
53      */
54     public static Drawable getmSystemIcon(Context context) {
55         if (mSystemIcon == null) {
56             return mSystemIcon = context.getResources().getDrawable(
57                     R.drawable.ic_system_red_dark);
58         } else {
59             return mSystemIcon;
60         }
61     }
62
63     /**
64      * @return the mSystemIcon
65      */
66     public static Drawable getmSystemIconDisable(Context context) {
67         if (mSystemIconDisable == null) {
68             return mSystemIconDisable = context.getResources().getDrawable(
69                     R.drawable.ic_system_red_disable);
70         } else {
71             return mSystemIconDisable;
72         }
73     }
74
75     /**
76      * Show Platform's Settings app Window with details about given packageName
77      * app
78      * 
79      * @param context
80      *            App context
81      * @param packageName
82      *            Package name of requested app details
83      */
84     public static void showInstalledAppDetails(Context context,
85             String packageName) {
86         Intent intent = new Intent();
87         final int apiLevel = Build.VERSION.SDK_INT;
88         if (apiLevel >= 9) { // above 2.3
89             intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
90             Uri uri = Uri.fromParts(SCHEME, packageName, null);
91             intent.setData(uri);
92         } else { // below 2.3
93             // TODO not tested
94             final String appPkgName = (apiLevel == 8 ? APP_PKG_NAME_22
95                     : APP_PKG_NAME_21);
96             intent.setAction(Intent.ACTION_VIEW);
97             intent.setClassName(APP_DETAILS_PACKAGE_NAME,
98                     APP_DETAILS_CLASS_NAME);
99             intent.putExtra(appPkgName, packageName);
100         }
101         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
102         context.startActivity(intent);
103     }
104
105     public static void unistallApp(Context context, String packageName) {
106         Intent intent = new Intent();
107         final int apiLevel = Build.VERSION.SDK_INT;
108         if (apiLevel >= 14) { // above 4.0
109             intent.setAction(Intent.ACTION_DELETE);
110             Uri uri = Uri.fromParts(SCHEME, packageName, null);
111             intent.setData(uri);
112         } else { // below 4.0
113             // TODO not tested
114             final String appPkgName = (apiLevel == 8 ? APP_PKG_NAME_22
115                     : APP_PKG_NAME_21);
116             intent.setAction(Intent.ACTION_VIEW);
117             intent.setClassName(APP_DETAILS_PACKAGE_NAME,
118                     APP_DETAILS_CLASS_NAME);
119             intent.putExtra(appPkgName, packageName);
120         }
121         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
122         context.startActivity(intent);
123     }
124
125     public static String getAppVersionCode(Context context) {
126         String versionName;
127         try {
128             versionName = context.getPackageManager().getPackageInfo(
129                     context.getPackageName(), 0).versionName;
130         } catch (NameNotFoundException e) {
131             versionName = "not found";
132             e.printStackTrace();
133         }
134         return versionName;
135     }
136
137     public static boolean isFirstRun(Context context) {
138         final SharedPreferences reader = context.getSharedPreferences(
139                 MY_PREFERENCES, Context.MODE_PRIVATE);
140         final boolean first = reader.getBoolean("is_first", true);
141         if (first) {
142             final SharedPreferences.Editor editor = reader.edit();
143             editor.putBoolean("is_first", false);
144             editor.commit();
145         }
146         return first;
147     }
148
149     public static void startBrowser(Context context, String url) {
150         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
151         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
152         context.startActivity(intent);
153     }
154 }