diff options
author | Luca Stefani <luca.stefani.ge1@gmail.com> | 2019-01-27 15:19:27 +0100 |
---|---|---|
committer | Luca Stefani <luca.stefani.ge1@gmail.com> | 2019-02-05 11:32:46 +0100 |
commit | 13e919f1be9e2204f85a7102581d7d232085c633 (patch) | |
tree | 1b30e610f9db0c32e24e82feea3f254434196c6b /src/com/android/launcher3 | |
parent | cbc5109d0da02f4c75ec21c22646fbbbfa8fece4 (diff) | |
download | android_packages_apps_Trebuchet-13e919f1be9e2204f85a7102581d7d232085c633.tar.gz android_packages_apps_Trebuchet-13e919f1be9e2204f85a7102581d7d232085c633.tar.bz2 android_packages_apps_Trebuchet-13e919f1be9e2204f85a7102581d7d232085c633.zip |
Trebuchet: Implement protected apps
Signed-off-by: Luca Stefani <luca.stefani.ge1@gmail.com>
Change-Id: Ide8f96ce360916fab44305c5f4a7c29ee31c179b
Diffstat (limited to 'src/com/android/launcher3')
12 files changed, 494 insertions, 256 deletions
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java index c3c4f5e5e..df64623ca 100644 --- a/src/com/android/launcher3/Launcher.java +++ b/src/com/android/launcher3/Launcher.java @@ -33,6 +33,7 @@ import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.annotation.TargetApi; import android.app.ActivityOptions; +import android.app.KeyguardManager; import android.appwidget.AppWidgetHostView; import android.appwidget.AppWidgetManager; import android.content.ActivityNotFoundException; @@ -249,6 +250,10 @@ public class Launcher extends BaseDraggingActivity implements LauncherExterns, private final Handler mHandler = new Handler(); private final Runnable mLogOnDelayedResume = this::logOnDelayedResume; + private static final int REQUEST_AUTH_CODE = 93; + private View mAuthView; + private ItemInfo mAuthInfo; + @Override protected void onCreate(Bundle savedInstanceState) { if (DEBUG_STRICT_MODE) { @@ -540,6 +545,13 @@ public class Launcher extends BaseDraggingActivity implements LauncherExterns, } }; + if (requestCode == REQUEST_AUTH_CODE) { + if (resultCode == RESULT_OK) { + startActivitySafely(mAuthView, requestArgs.getPendingIntent(), mAuthInfo); + } + return; + } + if (requestCode == REQUEST_BIND_APPWIDGET) { // This is called only if the user did not previously have permissions to bind widgets final int appWidgetId = data != null ? @@ -820,6 +832,29 @@ public class Launcher extends BaseDraggingActivity implements LauncherExterns, mStateManager.onWindowFocusChanged(); } + public void startActivitySafelyAuth(View v, Intent intent, ItemInfo item) { + KeyguardManager manager = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? + getSystemService(KeyguardManager.class) : + (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); + if (manager == null) { + throw new NullPointerException("No KeyguardManager found!"); + } + + String title = getString(R.string.trust_apps_manager_name); + String message = getString(R.string.trust_apps_auth_open_app, item.title); + Intent kmIntent = manager.createConfirmDeviceCredentialIntent(title, message); + + if (kmIntent != null) { + mAuthView = v; + mAuthInfo = item; + setWaitingForResult(PendingRequestArgs.forIntent(REQUEST_AUTH_CODE, intent, item)); + startActivityForResult(kmIntent, REQUEST_AUTH_CODE); + return; + } + + startActivitySafely(v, intent, item); + } + public interface LauncherOverlay { /** diff --git a/src/com/android/launcher3/SettingsActivity.java b/src/com/android/launcher3/SettingsActivity.java index bbb3eeff6..2ea7aa49b 100644 --- a/src/com/android/launcher3/SettingsActivity.java +++ b/src/com/android/launcher3/SettingsActivity.java @@ -54,7 +54,7 @@ import android.widget.NumberPicker; import com.android.launcher3.graphics.IconShapeOverride; import com.android.launcher3.lineage.LineageLauncherCallbacks; import com.android.launcher3.lineage.LineageUtils; -import com.android.launcher3.lineage.hidden.HiddenAppsActivity; +import com.android.launcher3.lineage.trust.TrustAppsActivity; import com.android.launcher3.notification.NotificationListener; import com.android.launcher3.util.ListViewHighlighter; import com.android.launcher3.util.SettingsObserver; @@ -190,10 +190,10 @@ public class SettingsActivity extends Activity { mGridPref.setSummary(mPrefs.getString(KEY_GRID_SIZE, getDefaultGridSize())); } - Preference hiddenApps = drawerGroup.findPreference("pref_hidden_apps"); - if (hiddenApps != null) { - hiddenApps.setOnPreferenceClickListener(preference -> { - Intent intent = new Intent(getActivity(), HiddenAppsActivity.class); + Preference trustApps = drawerGroup.findPreference("pref_trust_apps"); + if (trustApps != null) { + trustApps.setOnPreferenceClickListener(preference -> { + Intent intent = new Intent(getActivity(), TrustAppsActivity.class); startActivity(intent); return true; }); diff --git a/src/com/android/launcher3/lineage/hidden/UpdateItemVisibilityTask.java b/src/com/android/launcher3/lineage/hidden/UpdateItemVisibilityTask.java deleted file mode 100644 index b751fdc4f..000000000 --- a/src/com/android/launcher3/lineage/hidden/UpdateItemVisibilityTask.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (C) 2019 The LineageOS Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.android.launcher3.lineage.hidden; - -import android.os.AsyncTask; -import android.support.annotation.NonNull; - -import com.android.launcher3.lineage.hidden.db.HiddenComponent; -import com.android.launcher3.lineage.hidden.db.HiddenDatabaseHelper; - -public class UpdateItemVisibilityTask extends AsyncTask<HiddenComponent, Void, Boolean> { - @NonNull - private HiddenDatabaseHelper mDbHelper; - @NonNull - private UpdateCallback mCallback; - - UpdateItemVisibilityTask(@NonNull HiddenDatabaseHelper dbHelper, - @NonNull UpdateCallback callback) { - mDbHelper = dbHelper; - mCallback = callback; - } - - @Override - protected Boolean doInBackground(HiddenComponent... hiddenComponents) { - if (hiddenComponents.length < 1) { - return false; - } - - HiddenComponent component = hiddenComponents[0]; - String pkgName = component.getPackageName(); - - if (component.isHidden()) { - mDbHelper.addApp(pkgName); - } else { - mDbHelper.removeApp(pkgName); - } - - return true; - } - - @Override - protected void onPostExecute(Boolean result) { - mCallback.onUpdated(result); - } - - interface UpdateCallback { - void onUpdated(boolean result); - } -} diff --git a/src/com/android/launcher3/lineage/hidden/db/HiddenDatabaseHelper.java b/src/com/android/launcher3/lineage/hidden/db/HiddenDatabaseHelper.java deleted file mode 100644 index bfc4dfa30..000000000 --- a/src/com/android/launcher3/lineage/hidden/db/HiddenDatabaseHelper.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright (C) 2019 The LineageOS Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.android.launcher3.lineage.hidden.db; - -import android.content.ContentValues; -import android.content.Context; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteOpenHelper; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; - -public class HiddenDatabaseHelper extends SQLiteOpenHelper { - private static final int DATABASE_VERSION = 1; - private static final String DATABSE_NAME = "hidden_apps_db"; - - private static final String TABLE_NAME = "hidden_apps"; - private static final String KEY_UID = "uid"; - private static final String KEY_PKGNAME = "pkgname"; - - @Nullable - private static HiddenDatabaseHelper sSingleton; - - private HiddenDatabaseHelper(@NonNull Context context) { - super(context, DATABSE_NAME, null, DATABASE_VERSION); - } - - public static synchronized HiddenDatabaseHelper getInstance(@NonNull Context context) { - if (sSingleton == null) { - sSingleton = new HiddenDatabaseHelper(context); - } - - return sSingleton; - } - - @Override - public void onCreate(SQLiteDatabase db) { - String CMD_CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + - "(" + - KEY_UID + " INTEGER PRIMARY KEY AUTOINCREMENT," + - KEY_PKGNAME + " TEXT" + - ")"; - db.execSQL(CMD_CREATE_TABLE); - } - - @Override - public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - } - - public void addApp(@NonNull String packageName) { - if (isPackageHidden(packageName)) { - return; - } - - SQLiteDatabase db = getWritableDatabase(); - db.beginTransaction(); - - try { - ContentValues values = new ContentValues(); - values.put(KEY_PKGNAME, packageName); - - db.insertOrThrow(TABLE_NAME, null, values); - db.setTransactionSuccessful(); - } catch (Exception e) { - // Ignored - } finally { - db.endTransaction(); - } - } - - public void removeApp(@NonNull String packageName) { - if (!isPackageHidden(packageName)) { - return; - } - - SQLiteDatabase db = getWritableDatabase(); - db.beginTransaction(); - - try { - db.delete(TABLE_NAME, KEY_PKGNAME + "=?", new String[]{packageName}); - db.setTransactionSuccessful(); - } catch (Exception e) { - // Ignored - } finally { - db.endTransaction(); - } - } - - public boolean isPackageHidden(@NonNull String packageName) { - String query = String.format("SELECT * FROM %s WHERE %s = ?", TABLE_NAME, KEY_PKGNAME); - SQLiteDatabase db = getReadableDatabase(); - Cursor cursor = db.rawQuery(query, new String[]{packageName}); - boolean result = false; - try { - result = cursor.getCount() != 0; - } catch (Exception e) { - // Ignored - } finally { - if (cursor != null && !cursor.isClosed()) { - cursor.close(); - } - } - - return result; - } -} diff --git a/src/com/android/launcher3/lineage/hidden/HiddenAppsFilter.java b/src/com/android/launcher3/lineage/trust/HiddenAppsFilter.java index 47274f73e..942887693 100644 --- a/src/com/android/launcher3/lineage/hidden/HiddenAppsFilter.java +++ b/src/com/android/launcher3/lineage/trust/HiddenAppsFilter.java @@ -13,24 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.android.launcher3.lineage.hidden; +package com.android.launcher3.lineage.trust; import android.content.ComponentName; import android.content.Context; import com.android.launcher3.AppFilter; -import com.android.launcher3.lineage.hidden.db.HiddenDatabaseHelper; +import com.android.launcher3.lineage.trust.db.TrustDatabaseHelper; @SuppressWarnings("unused") public class HiddenAppsFilter extends AppFilter { - private HiddenDatabaseHelper mDbHelper; + private TrustDatabaseHelper mDbHelper; public HiddenAppsFilter(Context context) { if (context == null) { throw new IllegalArgumentException("Context must not be null!"); } - mDbHelper = HiddenDatabaseHelper.getInstance(context); + mDbHelper = TrustDatabaseHelper.getInstance(context); } @Override diff --git a/src/com/android/launcher3/lineage/hidden/LoadHiddenComponentsTask.java b/src/com/android/launcher3/lineage/trust/LoadTrustComponentsTask.java index 4636cebd7..f7d83a5b5 100644 --- a/src/com/android/launcher3/lineage/hidden/LoadHiddenComponentsTask.java +++ b/src/com/android/launcher3/lineage/trust/LoadTrustComponentsTask.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.android.launcher3.lineage.hidden; +package com.android.launcher3.lineage.trust; import android.content.Intent; import android.content.pm.PackageManager; @@ -23,16 +23,16 @@ import android.os.AsyncTask; import android.os.Build; import android.support.annotation.NonNull; -import com.android.launcher3.lineage.hidden.db.HiddenComponent; -import com.android.launcher3.lineage.hidden.db.HiddenDatabaseHelper; +import com.android.launcher3.lineage.trust.db.TrustComponent; +import com.android.launcher3.lineage.trust.db.TrustDatabaseHelper; import java.util.ArrayList; import java.util.Collections; import java.util.List; -public class LoadHiddenComponentsTask extends AsyncTask<Void, Integer, List<HiddenComponent>> { +public class LoadTrustComponentsTask extends AsyncTask<Void, Integer, List<TrustComponent>> { @NonNull - private HiddenDatabaseHelper mDbHelper; + private TrustDatabaseHelper mDbHelper; @NonNull private PackageManager mPackageManager; @@ -40,17 +40,17 @@ public class LoadHiddenComponentsTask extends AsyncTask<Void, Integer, List<Hidd @NonNull private Callback mCallback; - LoadHiddenComponentsTask(@NonNull HiddenDatabaseHelper dbHelper, - @NonNull PackageManager packageManager, - @NonNull Callback callback) { + LoadTrustComponentsTask(@NonNull TrustDatabaseHelper dbHelper, + @NonNull PackageManager packageManager, + @NonNull Callback callback) { mDbHelper = dbHelper; mPackageManager = packageManager; mCallback = callback; } @Override - protected List<HiddenComponent> doInBackground(Void... voids) { - List<HiddenComponent> list = new ArrayList<>(); + protected List<TrustComponent> doInBackground(Void... voids) { + List<TrustComponent> list = new ArrayList<>(); Intent filter = new Intent(Intent.ACTION_MAIN, null); filter.addCategory(Intent.CATEGORY_LAUNCHER); @@ -68,8 +68,9 @@ public class LoadHiddenComponentsTask extends AsyncTask<Void, Integer, List<Hidd PackageManager.GET_META_DATA)).toString(); Drawable icon = app.loadIcon(mPackageManager); boolean isHidden = mDbHelper.isPackageHidden(pkgName); + boolean isProtected = mDbHelper.isPackageProtected(pkgName); - list.add(new HiddenComponent(pkgName, icon, label, isHidden)); + list.add(new TrustComponent(pkgName, icon, label, isHidden, isProtected)); publishProgress(Math.round(i * 100f / numPackages)); } catch (PackageManager.NameNotFoundException ignored) { @@ -91,12 +92,12 @@ public class LoadHiddenComponentsTask extends AsyncTask<Void, Integer, List<Hidd } @Override - protected void onPostExecute(List<HiddenComponent> hiddenComponents) { - mCallback.onLoadCompleted(hiddenComponents); + protected void onPostExecute(List<TrustComponent> trustComponents) { + mCallback.onLoadCompleted(trustComponents); } interface Callback { void onLoadListProgress(int progress); - void onLoadCompleted(List<HiddenComponent> result); + void onLoadCompleted(List<TrustComponent> result); } } diff --git a/src/com/android/launcher3/lineage/hidden/HiddenAppsActivity.java b/src/com/android/launcher3/lineage/trust/TrustAppsActivity.java index bd7056417..95836f254 100644 --- a/src/com/android/launcher3/lineage/hidden/HiddenAppsActivity.java +++ b/src/com/android/launcher3/lineage/trust/TrustAppsActivity.java @@ -13,13 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.android.launcher3.lineage.hidden; +package com.android.launcher3.lineage.trust; import android.app.ActionBar; import android.app.Activity; +import android.app.AlertDialog; import android.app.KeyguardManager; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; import android.support.annotation.NonNull; @@ -27,6 +29,8 @@ import android.support.annotation.Nullable; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.view.Menu; +import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.LinearLayout; @@ -35,23 +39,29 @@ import android.widget.Toast; import com.android.launcher3.LauncherAppState; import com.android.launcher3.R; -import com.android.launcher3.lineage.hidden.db.HiddenComponent; -import com.android.launcher3.lineage.hidden.db.HiddenDatabaseHelper; +import com.android.launcher3.Utilities; +import com.android.launcher3.lineage.trust.db.TrustComponent; +import com.android.launcher3.lineage.trust.db.TrustDatabaseHelper; import java.util.List; -public class HiddenAppsActivity extends Activity implements - HiddenAppsAdapter.Listener, - LoadHiddenComponentsTask.Callback, - UpdateItemVisibilityTask.UpdateCallback { +import static com.android.launcher3.lineage.trust.db.TrustComponent.Kind.HIDDEN; +import static com.android.launcher3.lineage.trust.db.TrustComponent.Kind.PROTECTED; + +public class TrustAppsActivity extends Activity implements + TrustAppsAdapter.Listener, + LoadTrustComponentsTask.Callback, + UpdateItemTask.UpdateCallback { + private static final int REQUEST_AUTH_CODE = 92; + private static final String KEY_TRUST_ONBOARDING = "pref_trust_onboarding"; private RecyclerView mRecyclerView; private LinearLayout mLoadingView; private ProgressBar mProgressBar; - private HiddenDatabaseHelper mDbHelper; - private HiddenAppsAdapter mAdapter; + private TrustDatabaseHelper mDbHelper; + private TrustAppsAdapter mAdapter; @Override protected void onCreate(@Nullable Bundle savedInstance) { @@ -67,8 +77,8 @@ public class HiddenAppsActivity extends Activity implements mLoadingView = findViewById(R.id.hidden_apps_loading); mProgressBar = findViewById(R.id.hidden_apps_progress_bar); - mAdapter = new HiddenAppsAdapter(this); - mDbHelper = HiddenDatabaseHelper.getInstance(this); + mAdapter = new TrustAppsAdapter(this); + mDbHelper = TrustDatabaseHelper.getInstance(this); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setItemAnimator(new DefaultItemAnimator()); @@ -91,18 +101,34 @@ public class HiddenAppsActivity extends Activity implements } @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater menuInflater = getMenuInflater(); + menuInflater.inflate(R.menu.menu_trust_apps, menu); + return super.onCreateOptionsMenu(menu); + } + + @Override public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == android.R.id.home) { + int id = item.getItemId(); + if (id == android.R.id.home) { finish(); return true; + } else if (id == R.id.menu_trust_help) { + showOnBoarding(true); + return true; + } else { + return super.onOptionsItemSelected(item); } + } - return super.onOptionsItemSelected(item); + @Override + public void onHiddenItemChanged(@NonNull TrustComponent component) { + new UpdateItemTask(mDbHelper, this, HIDDEN).execute(component); } @Override - public void onItemChanged(@NonNull HiddenComponent component) { - new UpdateItemVisibilityTask(mDbHelper, this).execute(component); + public void onProtectedItemChanged(@NonNull TrustComponent component) { + new UpdateItemTask(mDbHelper, this, PROTECTED).execute(component); } @Override @@ -119,7 +145,7 @@ public class HiddenAppsActivity extends Activity implements } @Override - public void onLoadCompleted(List<HiddenComponent> result) { + public void onLoadCompleted(List<TrustComponent> result) { mLoadingView.setVisibility(View.GONE); mRecyclerView.setVisibility(View.VISIBLE); mAdapter.update(result); @@ -133,8 +159,8 @@ public class HiddenAppsActivity extends Activity implements throw new NullPointerException("No KeyguardManager found!"); } - String title = getString(R.string.hidden_apps_manager_name); - String message = getString(R.string.hidden_apps_auth_manager); + String title = getString(R.string.trust_apps_manager_name); + String message = getString(R.string.trust_apps_auth_manager); Intent intent = manager.createConfirmDeviceCredentialIntent(title, message); if (intent != null) { @@ -142,7 +168,7 @@ public class HiddenAppsActivity extends Activity implements return; } - Toast.makeText(this, R.string.hidden_apps_no_lock_error, + Toast.makeText(this, R.string.trust_apps_no_lock_error, Toast.LENGTH_LONG).show(); finish(); } @@ -150,6 +176,24 @@ public class HiddenAppsActivity extends Activity implements private void showUi() { mLoadingView.setVisibility(View.VISIBLE); - new LoadHiddenComponentsTask(mDbHelper, getPackageManager(), this).execute(); + showOnBoarding(false); + + new LoadTrustComponentsTask(mDbHelper, getPackageManager(), this).execute(); + } + + private void showOnBoarding(boolean forceShow) { + SharedPreferences preferenceManager = Utilities.getPrefs(this); + if (!forceShow && preferenceManager.getBoolean(KEY_TRUST_ONBOARDING, false)) { + return; + } + + preferenceManager.edit() + .putBoolean(KEY_TRUST_ONBOARDING, true) + .apply(); + + new AlertDialog.Builder(this) + .setView(R.layout.dialog_trust_welcome) + .setPositiveButton(android.R.string.ok, null) + .show(); } } diff --git a/src/com/android/launcher3/lineage/hidden/HiddenAppsAdapter.java b/src/com/android/launcher3/lineage/trust/TrustAppsAdapter.java index acb07ee90..82a6587a6 100644 --- a/src/com/android/launcher3/lineage/hidden/HiddenAppsAdapter.java +++ b/src/com/android/launcher3/lineage/trust/TrustAppsAdapter.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.android.launcher3.lineage.hidden; +package com.android.launcher3.lineage.trust; import android.graphics.drawable.Animatable2; import android.graphics.drawable.AnimatedVectorDrawable; @@ -29,20 +29,20 @@ import android.widget.ImageView; import android.widget.TextView; import com.android.launcher3.R; -import com.android.launcher3.lineage.hidden.db.HiddenComponent; +import com.android.launcher3.lineage.trust.db.TrustComponent; import java.util.ArrayList; import java.util.List; -class HiddenAppsAdapter extends RecyclerView.Adapter<HiddenAppsAdapter.ViewHolder> { - private List<HiddenComponent> mList = new ArrayList<>(); +class TrustAppsAdapter extends RecyclerView.Adapter<TrustAppsAdapter.ViewHolder> { + private List<TrustComponent> mList = new ArrayList<>(); private Listener mListener; - HiddenAppsAdapter(Listener listener) { + TrustAppsAdapter(Listener listener) { mListener = listener; } - public void update(List<HiddenComponent> list) { + public void update(List<TrustComponent> list) { DiffUtil.DiffResult result = DiffUtil.calculateDiff(new Callback(mList, list)); mList = list; result.dispatchUpdatesTo(this); @@ -66,64 +66,102 @@ class HiddenAppsAdapter extends RecyclerView.Adapter<HiddenAppsAdapter.ViewHolde } public interface Listener { - void onItemChanged(@NonNull HiddenComponent component); + void onHiddenItemChanged(@NonNull TrustComponent component); + + void onProtectedItemChanged(@NonNull TrustComponent component); } class ViewHolder extends RecyclerView.ViewHolder { private ImageView mIconView; private TextView mLabelView; - private ImageView mLockView; + private ImageView mHiddenView; + private ImageView mProtectedView; ViewHolder(@NonNull View itemView) { super(itemView); mIconView = itemView.findViewById(R.id.item_hidden_app_icon); mLabelView = itemView.findViewById(R.id.item_hidden_app_title); - mLockView = itemView.findViewById(R.id.item_hidden_app_switch); + mHiddenView = itemView.findViewById(R.id.item_hidden_app_switch); + mProtectedView = itemView.findViewById(R.id.item_protected_app_switch); } - void bind(HiddenComponent component) { + void bind(TrustComponent component) { mIconView.setImageDrawable(component.getIcon()); mLabelView.setText(component.getLabel()); - mLockView.setImageResource(component.isHidden() ? + + mHiddenView.setImageResource(component.isHidden() ? R.drawable.ic_hidden_locked : R.drawable.ic_hidden_unlocked); + mProtectedView.setImageResource(component.isProtected() ? + R.drawable.ic_protected_locked : R.drawable.ic_protected_unlocked); - itemView.setOnClickListener(v -> { + mHiddenView.setOnClickListener(v -> { component.invertVisibility(); - mLockView.setImageResource(component.isHidden() ? + mHiddenView.setImageResource(component.isHidden() ? R.drawable.avd_hidden_lock : R.drawable.avd_hidden_unlock); - AnimatedVectorDrawable avd = (AnimatedVectorDrawable) mLockView.getDrawable(); + AnimatedVectorDrawable avd = (AnimatedVectorDrawable) mHiddenView.getDrawable(); int position = getAdapterPosition(); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) { avd.registerAnimationCallback(new Animatable2.AnimationCallback() { @Override public void onAnimationEnd(Drawable drawable) { - updateList(position, component); + updateHiddenList(position, component); } }); avd.start(); } else { avd.start(); - updateList(position, component); + updateHiddenList(position, component); } }); + + mProtectedView.setOnClickListener(v -> { + component.invertProtection(); + + mProtectedView.setImageResource(component.isProtected() ? + R.drawable.avd_protected_lock : R.drawable.avd_protected_unlock); + AnimatedVectorDrawable avd = (AnimatedVectorDrawable) mProtectedView.getDrawable(); + + int position = getAdapterPosition(); + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) { + avd.registerAnimationCallback(new Animatable2.AnimationCallback() { + @Override + public void onAnimationEnd(Drawable drawable) { + updateProtectedList(position, component); + } + }); + avd.start(); + } else { + avd.start(); + updateProtectedList(position, component); + } + }); + } + + private void updateHiddenList(int position, TrustComponent component) { + mListener.onHiddenItemChanged(component); + updateList(position, component); + } + + private void updateProtectedList(int position, TrustComponent component) { + mListener.onProtectedItemChanged(component); + updateList(position, component); } - private void updateList(int position, HiddenComponent component) { - mListener.onItemChanged(component); + private void updateList(int position, TrustComponent component) { mList.set(position, component); notifyItemChanged(position); } } private static class Callback extends DiffUtil.Callback { - List<HiddenComponent> mOldList; - List<HiddenComponent> mNewList; + List<TrustComponent> mOldList; + List<TrustComponent> mNewList; - public Callback(List<HiddenComponent> oldList, - List<HiddenComponent> newList) { + public Callback(List<TrustComponent> oldList, + List<TrustComponent> newList) { mOldList = oldList; mNewList = newList; } diff --git a/src/com/android/launcher3/lineage/trust/UpdateItemTask.java b/src/com/android/launcher3/lineage/trust/UpdateItemTask.java new file mode 100644 index 000000000..e81176206 --- /dev/null +++ b/src/com/android/launcher3/lineage/trust/UpdateItemTask.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2019 The LineageOS Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.launcher3.lineage.trust; + +import android.os.AsyncTask; +import android.support.annotation.NonNull; + +import com.android.launcher3.lineage.trust.db.TrustComponent; +import com.android.launcher3.lineage.trust.db.TrustDatabaseHelper; + +public class UpdateItemTask extends AsyncTask<TrustComponent, Void, Boolean> { + @NonNull + private TrustDatabaseHelper mDbHelper; + @NonNull + private UpdateCallback mCallback; + @NonNull + private TrustComponent.Kind mKind; + + UpdateItemTask(@NonNull TrustDatabaseHelper dbHelper, + @NonNull UpdateCallback callback, + @NonNull TrustComponent.Kind kind) { + mDbHelper = dbHelper; + mCallback = callback; + mKind = kind; + } + + @Override + protected Boolean doInBackground(TrustComponent... trustComponents) { + if (trustComponents.length < 1) { + return false; + } + + TrustComponent component = trustComponents[0]; + String pkgName = component.getPackageName(); + + switch (mKind) { + case HIDDEN: + if (component.isHidden()) { + mDbHelper.addHiddenApp(pkgName); + } else { + mDbHelper.removeHiddenApp(pkgName); + } + break; + case PROTECTED: + if (component.isProtected()) { + mDbHelper.addProtectedApp(pkgName); + } else { + mDbHelper.removeProtectedApp(pkgName); + } + break; + } + return true; + } + + @Override + protected void onPostExecute(Boolean result) { + mCallback.onUpdated(result); + } + + interface UpdateCallback { + void onUpdated(boolean result); + } +} diff --git a/src/com/android/launcher3/lineage/hidden/db/HiddenComponent.java b/src/com/android/launcher3/lineage/trust/db/TrustComponent.java index d95d5c823..ff8976d02 100644 --- a/src/com/android/launcher3/lineage/hidden/db/HiddenComponent.java +++ b/src/com/android/launcher3/lineage/trust/db/TrustComponent.java @@ -13,23 +13,29 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.android.launcher3.lineage.hidden.db; +package com.android.launcher3.lineage.trust.db; import android.graphics.drawable.Drawable; import android.support.annotation.NonNull; -public class HiddenComponent { - @NonNull private final String mPackageName; - @NonNull private final Drawable mIcon; - @NonNull private final String mLabel; +public class TrustComponent { + @NonNull + private final String mPackageName; + @NonNull + private final Drawable mIcon; + @NonNull + private final String mLabel; + private boolean mIsHidden; + private boolean mIsProtected; - public HiddenComponent(@NonNull String packageName, @NonNull Drawable icon, - @NonNull String label, boolean isHidden) { + public TrustComponent(@NonNull String packageName, @NonNull Drawable icon, + @NonNull String label, boolean isHidden, boolean isProtected) { mPackageName = packageName; mIcon = icon; mLabel = label; mIsHidden = isHidden; + mIsProtected = isProtected; } @NonNull @@ -51,17 +57,25 @@ public class HiddenComponent { return mIsHidden; } + public boolean isProtected() { + return mIsProtected; + } + public void invertVisibility() { mIsHidden = !mIsHidden; } + public void invertProtection() { + mIsProtected = !mIsProtected; + } + @Override public boolean equals(Object other) { - if (!(other instanceof HiddenComponent)) { + if (!(other instanceof TrustComponent)) { return false; } - HiddenComponent otherComponent = (HiddenComponent) other; + TrustComponent otherComponent = (TrustComponent) other; return otherComponent.getPackageName().equals(mPackageName) && otherComponent.isHidden() == mIsHidden; } @@ -70,4 +84,9 @@ public class HiddenComponent { public int hashCode() { return mPackageName.hashCode() + (mIsHidden ? 1 : 0); } + + public enum Kind { + HIDDEN, + PROTECTED, + } } diff --git a/src/com/android/launcher3/lineage/trust/db/TrustDatabaseHelper.java b/src/com/android/launcher3/lineage/trust/db/TrustDatabaseHelper.java new file mode 100644 index 000000000..64d169561 --- /dev/null +++ b/src/com/android/launcher3/lineage/trust/db/TrustDatabaseHelper.java @@ -0,0 +1,197 @@ +/* + * Copyright (C) 2019 The LineageOS Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.launcher3.lineage.trust.db; + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +public class TrustDatabaseHelper extends SQLiteOpenHelper { + private static final int DATABASE_VERSION = 1; + private static final String DATABASE_NAME = "trust_apps_db"; + + private static final String TABLE_NAME = "trust_apps"; + private static final String KEY_UID = "uid"; + private static final String KEY_PKGNAME = "pkgname"; + private static final String KEY_HIDDEN = "hidden"; + private static final String KEY_PROTECTED = "protected"; + + @Nullable + private static TrustDatabaseHelper sSingleton; + + private TrustDatabaseHelper(@NonNull Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + } + + public static synchronized TrustDatabaseHelper getInstance(@NonNull Context context) { + if (sSingleton == null) { + sSingleton = new TrustDatabaseHelper(context); + } + + return sSingleton; + } + + @Override + public void onCreate(SQLiteDatabase db) { + String CMD_CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + + "(" + + KEY_UID + " INTEGER PRIMARY KEY AUTOINCREMENT," + + KEY_PKGNAME + " TEXT," + + KEY_HIDDEN + " INTEGER DEFAULT 0," + + KEY_PROTECTED + " INTEGER DEFAULT 0" + + ")"; + db.execSQL(CMD_CREATE_TABLE); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + } + + public void addHiddenApp(@NonNull String packageName) { + if (isPackageHidden(packageName)) { + return; + } + + SQLiteDatabase db = getWritableDatabase(); + db.beginTransaction(); + + try { + ContentValues values = new ContentValues(); + values.put(KEY_PKGNAME, packageName); + values.put(KEY_HIDDEN, 1); + + int rows = db.update(TABLE_NAME, values, KEY_PKGNAME + " = ?", new String[]{KEY_PKGNAME}); + if (rows != 1) { + // Entry doesn't exist, create a new one + db.insertOrThrow(TABLE_NAME, null, values); + } + db.setTransactionSuccessful(); + } catch (Exception e) { + // Ignored + } finally { + db.endTransaction(); + } + } + + public void addProtectedApp(@NonNull String packageName) { + if (isPackageProtected(packageName)) { + return; + } + + SQLiteDatabase db = getWritableDatabase(); + db.beginTransaction(); + + try { + ContentValues values = new ContentValues(); + values.put(KEY_PKGNAME, packageName); + values.put(KEY_PROTECTED, 1); + + int rows = db.update(TABLE_NAME, values, KEY_PKGNAME + " = ?", new String[]{KEY_PKGNAME}); + if (rows != 1) { + // Entry doesn't exist, create a new one + db.insertOrThrow(TABLE_NAME, null, values); + } + db.setTransactionSuccessful(); + } catch (Exception e) { + // Ignored + } finally { + db.endTransaction(); + } + } + + + public void removeHiddenApp(@NonNull String packageName) { + if (!isPackageHidden(packageName)) { + return; + } + + SQLiteDatabase db = getWritableDatabase(); + db.beginTransaction(); + + try { + ContentValues values = new ContentValues(); + values.put(KEY_HIDDEN, 0); + + db.update(TABLE_NAME, values, KEY_PKGNAME + " = ?", new String[]{packageName}); + db.setTransactionSuccessful(); + } catch (Exception e) { + // Ignored + } finally { + db.endTransaction(); + } + } + + public void removeProtectedApp(@NonNull String packageName) { + if (!isPackageProtected(packageName)) { + return; + } + + SQLiteDatabase db = getWritableDatabase(); + db.beginTransaction(); + + try { + ContentValues values = new ContentValues(); + values.put(KEY_PROTECTED, 0); + + db.update(TABLE_NAME, values, KEY_PKGNAME + " = ?", new String[]{packageName}); + db.setTransactionSuccessful(); + } catch (Exception e) { + // Ignored + } finally { + db.endTransaction(); + } + } + + public boolean isPackageHidden(@NonNull String packageName) { + String query = String.format("SELECT * FROM %s WHERE %s = ? AND %s = ?", TABLE_NAME, KEY_PKGNAME, KEY_HIDDEN); + SQLiteDatabase db = getReadableDatabase(); + Cursor cursor = db.rawQuery(query, new String[]{packageName, String.valueOf(1)}); + boolean result = false; + try { + result = cursor.getCount() != 0; + } catch (Exception e) { + // Ignored + } finally { + if (cursor != null && !cursor.isClosed()) { + cursor.close(); + } + } + + return result; + } + + public boolean isPackageProtected(@NonNull String packageName) { + String query = String.format("SELECT * FROM %s WHERE %s = ? AND %s = ?", TABLE_NAME, KEY_PKGNAME, KEY_PROTECTED); + SQLiteDatabase db = getReadableDatabase(); + Cursor cursor = db.rawQuery(query, new String[]{packageName, String.valueOf(1)}); + boolean result = false; + try { + result = cursor.getCount() != 0; + } catch (Exception e) { + // Ignored + } finally { + if (cursor != null && !cursor.isClosed()) { + cursor.close(); + } + } + + return result; + } +} diff --git a/src/com/android/launcher3/touch/ItemClickHandler.java b/src/com/android/launcher3/touch/ItemClickHandler.java index f2f5592e5..fbf10cfe4 100644 --- a/src/com/android/launcher3/touch/ItemClickHandler.java +++ b/src/com/android/launcher3/touch/ItemClickHandler.java @@ -44,6 +44,7 @@ import com.android.launcher3.ShortcutInfo; import com.android.launcher3.compat.AppWidgetManagerCompat; import com.android.launcher3.folder.Folder; import com.android.launcher3.folder.FolderIcon; +import com.android.launcher3.lineage.trust.db.TrustDatabaseHelper; import com.android.launcher3.util.PackageManagerHelper; import com.android.launcher3.widget.PendingAppWidgetHostView; import com.android.launcher3.widget.WidgetAddFlowHandler; @@ -227,6 +228,14 @@ public class ItemClickHandler { intent.setPackage(null); } } - launcher.startActivitySafely(v, intent, item); + + TrustDatabaseHelper db = TrustDatabaseHelper.getInstance(launcher); + boolean isProtected = db.isPackageProtected(item.getTargetComponent().getPackageName()); + + if (isProtected) { + launcher.startActivitySafelyAuth(v, intent, item); + } else { + launcher.startActivitySafely(v, intent, item); + } } } |