checkey / src / info / guardianproject / checkey / AppEntry.java @ f721b462
History | View | Annotate | Download (2.94 KB)
| 1 |
/*
|
|---|---|
| 2 |
Licensed under the Apache License, Version 2.0 (the "License");
|
| 3 |
you may not use this file except in compliance with the License.
|
| 4 |
You may obtain a copy of the License at
|
| 5 |
|
| 6 |
http://www.apache.org/licenses/LICENSE-2.0
|
| 7 |
|
| 8 |
Unless required by applicable law or agreed to in writing, software
|
| 9 |
distributed under the License is distributed on an "AS IS" BASIS,
|
| 10 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 11 |
See the License for the specific language governing permissions and
|
| 12 |
limitations under the License.
|
| 13 |
|
| 14 |
Based on Paul Blundell's Tutorial:
|
| 15 |
http://blog.blundell-apps.com/tut-asynctask-loader-using-support-library/
|
| 16 |
|
| 17 |
which is originally based on:
|
| 18 |
https://developer.android.com/reference/android/content/AsyncTaskLoader.html
|
| 19 |
*/
|
| 20 |
|
| 21 |
package info.guardianproject.checkey; |
| 22 |
|
| 23 |
import android.content.pm.ApplicationInfo; |
| 24 |
import android.content.pm.PackageManager; |
| 25 |
import android.graphics.drawable.Drawable; |
| 26 |
|
| 27 |
import java.io.File; |
| 28 |
|
| 29 |
public class AppEntry { |
| 30 |
public static final String TAG = "AppEntry"; |
| 31 |
|
| 32 |
private final AppListLoader loader; |
| 33 |
private final ApplicationInfo info; |
| 34 |
private final File apkFile; |
| 35 |
private boolean enabled; |
| 36 |
private String label; |
| 37 |
private Drawable icon;
|
| 38 |
private boolean mounted; |
| 39 |
|
| 40 |
public AppEntry(AppListLoader loader, ApplicationInfo info) {
|
| 41 |
this.loader = loader;
|
| 42 |
this.info = info;
|
| 43 |
apkFile = new File(info.sourceDir); |
| 44 |
} |
| 45 |
|
| 46 |
public ApplicationInfo getApplicationInfo() {
|
| 47 |
return info;
|
| 48 |
} |
| 49 |
|
| 50 |
public String getPackageName() { |
| 51 |
return info.packageName;
|
| 52 |
} |
| 53 |
|
| 54 |
public File getApkFile() { |
| 55 |
return apkFile;
|
| 56 |
} |
| 57 |
|
| 58 |
public String getLabel() { |
| 59 |
return label;
|
| 60 |
} |
| 61 |
|
| 62 |
public boolean isEnabled() { |
| 63 |
return enabled;
|
| 64 |
} |
| 65 |
|
| 66 |
public void setEnabled(boolean enabled) { |
| 67 |
this.enabled = enabled;
|
| 68 |
} |
| 69 |
|
| 70 |
public Drawable getIcon() {
|
| 71 |
if (icon == null) { |
| 72 |
if (apkFile.exists()) {
|
| 73 |
icon = info.loadIcon(loader.pm); |
| 74 |
return icon;
|
| 75 |
} else {
|
| 76 |
mounted = false;
|
| 77 |
} |
| 78 |
} else if (!mounted) { |
| 79 |
// If the app wasn't mounted but is now mounted, reload its icon
|
| 80 |
if (apkFile.exists()) {
|
| 81 |
mounted = true;
|
| 82 |
icon = info.loadIcon(loader.pm); |
| 83 |
} |
| 84 |
} else {
|
| 85 |
return icon;
|
| 86 |
} |
| 87 |
|
| 88 |
return loader.getContext().getResources().getDrawable(android.R.drawable.sym_def_app_icon);
|
| 89 |
} |
| 90 |
|
| 91 |
@Override
|
| 92 |
public String toString() { |
| 93 |
return label;
|
| 94 |
} |
| 95 |
|
| 96 |
public void loadLabel(PackageManager pm) { |
| 97 |
if (label == null || !mounted) { |
| 98 |
if (apkFile.exists()) {
|
| 99 |
mounted = true;
|
| 100 |
CharSequence label = null; |
| 101 |
label = info.loadLabel(pm); |
| 102 |
this.label = label != null ? label.toString() : info.packageName; |
| 103 |
} else {
|
| 104 |
mounted = false;
|
| 105 |
label = info.packageName; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |