Skip to content

Commit

Permalink
#105: app in mode Protect (App Pinning): only few non-changing comman…
Browse files Browse the repository at this point in the history
…ds are allowed
  • Loading branch information
k3b committed Jan 8, 2018
1 parent 8513782 commit 142f550
Show file tree
Hide file tree
Showing 17 changed files with 192 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ protected void onPostExecute(IDirectory directoryRoot) {
this.mMustShowNavigator = false;
final FragmentManager manager = getFragmentManager();
DirectoryPickerFragment dirDialog = new DirectoryPickerFragment(); // (DirectoryPickerFragment) manager.findFragmentByTag(DLG_NAVIGATOR_TAG);
dirDialog.setContextMenuId(Global.locked ? R.menu.menu_context_dir_locked : R.menu.menu_context_dirpicker);
dirDialog.setContextMenuId(LockScreen.isLocked(this) ? R.menu.menu_context_dir_locked : R.menu.menu_context_dirpicker);

dirDialog.defineDirectoryNavigation(mDirectoryRoot, dirQueryID,
this.mGalleryQueryParameter.mCurrentPathFromFolderPicker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ private void onDirectoryDataLoadComplete(IDirectory directoryRoot, int queryId)
dirInfo.directoryRoot = directoryRoot;
final FragmentManager manager = getFragmentManager();
DirectoryPickerFragment dlg = new DirectoryPickerFragment();
dlg.setContextMenuId(Global.locked ? R.menu.menu_context_dir_locked : R.menu.menu_context_dirpicker);
dlg.setContextMenuId(LockScreen.isLocked(this) ? R.menu.menu_context_dir_locked : R.menu.menu_context_dirpicker);

dlg.defineDirectoryNavigation(dirInfo.directoryRoot, dirInfo.queryId, dirInfo.currentPath);

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/de/k3b/android/androFotoFinder/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public static void fixMenu(Context context, Menu menu) {
R.id.cmd_more,
R.id.cmd_show_geo,
R.id.cmd_gallery,
R.id.cmd_unlock2,
R.id.cmd_lock,
R.id.cmd_show_geo_as
);
}
Expand Down
46 changes: 42 additions & 4 deletions app/src/main/java/de/k3b/android/androFotoFinder/LockScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,63 @@

package de.k3b.android.androFotoFinder;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.view.Menu;
import android.view.MenuItem;

/**
* Management of app locking (aka Android "Screen"-pinning, "Kiosk Mode", "LockTask").
*
* Encapsulates special handling for android-4.0-4.4; 5.0; 6.0ff
*
* Created by k3b on 28.12.2017.
*/

@TargetApi(Build.VERSION_CODES.M)
public class LockScreen {
private static boolean OS_APPLOCK_ENABLED = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);

public static boolean onOptionsItemSelected(Activity parent, MenuItem item) {
switch (item.getItemId()) {
case R.id.cmd_lock:
Global.locked = true;
SettingsActivity.global2Prefs(parent.getApplication());
if (!isLocked(parent)) {
if (OS_APPLOCK_ENABLED) {
parent.startLockTask();
} else {
Global.locked = true;
SettingsActivity.global2Prefs(parent.getApplication());
}
}
return true;
case R.id.cmd_unlock:
case R.id.cmd_unlock2:
// only for old android (< 5.0). Else use app-pinning-end
Global.locked = false;
SettingsActivity.global2Prefs(parent.getApplication());
return true;
}
return false;
}

public static boolean isLocked(Context ctx) {
if (OS_APPLOCK_ENABLED) {
ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return activityManager.getLockTaskModeState() != ActivityManager.LOCK_TASK_MODE_NONE;
}

// deprecated
return activityManager.isInLockTaskMode();
}
return Global.locked;
}

public static void fixMenu(Menu menu) {
if ((menu != null) && OS_APPLOCK_ENABLED) {
MenuItem unlock = menu.findItem(R.id.cmd_unlock2);
if (unlock != null) menu.removeItem(R.id.cmd_unlock2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@
* to handle interaction events.
* Use the {@link GalleryCursorFragment#newInstance} factory method to
* create an instance of this fragment.
*
* States view-locked <=> view <=> view-multiselect
* pick-single, pick-multible, pick-locked
*/
public class GalleryCursorFragment extends Fragment implements Queryable, DirectoryGui,Common, TagsPickerFragment.ITagsPicker {
private static final String INSTANCE_STATE_LAST_VISIBLE_POSITION = "lastVisiblePosition";
Expand All @@ -111,6 +114,7 @@ public class GalleryCursorFragment extends Fragment implements Queryable, Direc

private static int nextLoaderID = 100;
private int loaderID = -1;
private boolean locked = false; // if != Global.locked : must update menu

private HorizontalScrollView mParentPathBarScroller;
private LinearLayout mParentPathBar;
Expand Down Expand Up @@ -452,7 +456,9 @@ public void onResume() {
Global.debugMemory(mDebugPrefix, "onResume");
super.onResume(); // this may destroy an other instance of gallery(fragment)

if (Global.locked) {
final boolean locked = LockScreen.isLocked(this.getActivity());
if (this.locked != locked) {
this.locked = locked;
mMustReplaceMenue = true;
getActivity().invalidateOptionsMenu();
}
Expand All @@ -470,6 +476,24 @@ public void onResume() {

}

/**
* Call back from sub-activities.<br/>
* Process Change StartTime (longpress start), Select StopTime before stop
* (longpress stop) or filter change for detailReport
*/
@Override
public void onActivityResult(final int requestCode,
final int resultCode, final Intent intent) {
super.onActivityResult(requestCode,resultCode,intent);

final boolean locked = LockScreen.isLocked(this.getActivity());
if (this.locked != locked) {
this.locked = locked;
mMustReplaceMenue = true;
getActivity().invalidateOptionsMenu();
}
}

@Override
public void onDetach() {
Global.debugMemory(mDebugPrefix, "onDetach");
Expand Down Expand Up @@ -575,7 +599,7 @@ public String toString() {
return mDebugPrefix + this.mAdapter;
}

/*********************** local helper *******************************************/
/* --********************** local helper ****************************************** - */
/** an Image in the FotoGallery was clicked */
private void onGalleryImageClick(final GalleryCursorAdapter.GridCellViewHolder holder, int position) {
if ((!multiSelectionHandleClick(holder)) && (mGalleryListener != null)) {
Expand Down Expand Up @@ -706,7 +730,7 @@ private static String getDirectoryDisplayText(String prefix, IDirectory director

/** starts mutliselection */
private boolean onGalleryLongImageClick(final GalleryCursorAdapter.GridCellViewHolder holder, int position) {
if (!Global.locked) {
if (!LockScreen.isLocked(this.getActivity())) {
if (!isMultiSelectionActive()) {
startMultiSelectionMode();

Expand All @@ -717,10 +741,9 @@ private boolean onGalleryLongImageClick(final GalleryCursorAdapter.GridCellViewH
// in gallery mode long click is view image
ImageDetailActivityViewPager.showActivity(this.getActivity(), getUri(holder.imageID), position, getCurrentQuery(), ImageDetailActivityViewPager.ACTIVITY_ID);
}
} if (isMultiSelectionActive()) {
mSelectedItems.clear();
return true;
}
return true;
return false; // no multi-selection in lock mode
}

private void startMultiSelectionMode() {
Expand All @@ -734,36 +757,53 @@ private void startMultiSelectionMode() {
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuInflater inflater = getActivity().getMenuInflater();
if (mMustReplaceMenue) {
final boolean locked = LockScreen.isLocked(this.getActivity());
if (mMustReplaceMenue || (locked != this.locked)) {
MenuInflater inflater = getActivity().getMenuInflater();

this.locked = locked;
mMustReplaceMenue = false;
menu.clear();
if (Global.locked) {
inflater.inflate(R.menu.menu_gallery_locked, menu);
} else if (mode == MODE_VIEW) {
inflater.inflate(R.menu.menu_gallery_multiselect_mode_all, menu);
mShareOnlyToggle = menu.findItem(R.id.cmd_selected_only);
if (mShowSelectedOnly) {
mShareOnlyToggle.setIcon(android.R.drawable.checkbox_on_background);
mShareOnlyToggle.setChecked(true);
} else { // if (mode != MODE_VIEW) {
mMenuRemoveAllSelected = null;
if (mode == MODE_VIEW) {
if (locked) { // view-locked
mSelectedItems.clear();
inflater.inflate(R.menu.menu_gallery_locked, menu);
LockScreen.fixMenu(menu);
} else if (isMultiSelectionActive()) { // view-multiselect
inflater.inflate(R.menu.menu_gallery_multiselect_mode_all, menu);

mShareOnlyToggle = menu.findItem(R.id.cmd_selected_only);
if (mShowSelectedOnly && (mShareOnlyToggle != null)) {
mShareOnlyToggle.setIcon(android.R.drawable.checkbox_on_background);
mShareOnlyToggle.setChecked(true);
}
MenuItem shareItem = menu.findItem(R.id.menu_item_share);
shareItem.setActionProvider(mShareActionProvider);
// multiSelectionUpdateShareIntent();
inflater.inflate(R.menu.menu_image_commands, menu);
multiSelectionUpdateShareIntent();
Global.fixMenu(getActivity(), menu);

} else { // view-non-select
inflater.inflate(R.menu.menu_gallery_non_selected_only, menu);
inflater.inflate(R.menu.menu_gallery_non_multiselect, menu);
Global.fixMenu(getActivity(), menu);
}
MenuItem shareItem = menu.findItem(R.id.menu_item_share);
shareItem.setActionProvider(mShareActionProvider);
// multiSelectionUpdateShareIntent();
inflater.inflate(R.menu.menu_image_commands, menu);

multiSelectionUpdateShareIntent();
Global.fixMenu(getActivity(), menu);

} else {
inflater.inflate(R.menu.menu_gallery_pick, menu);
if (locked) { // pick-locked
mSelectedItems.clear();
inflater.inflate(R.menu.menu_gallery_locked, menu);
} else { // pick-single/multible
inflater.inflate(R.menu.menu_gallery_non_multiselect, menu);
}
}
mMenuRemoveAllSelected = menu.findItem(R.id.cmd_selection_remove_all);
}

mMenuRemoveAllSelected = menu.findItem(R.id.cmd_selection_remove_all);

updateSelectionCount();
}
Expand Down Expand Up @@ -960,7 +1000,7 @@ private boolean cmdMoveOrCopyWithDestDirPicker(final boolean move, String lastCo
destDir.defineDirectoryNavigation(OsUtils.getRootOSDirectory(),
(move) ? FotoSql.QUERY_TYPE_GROUP_MOVE : FotoSql.QUERY_TYPE_GROUP_COPY,
lastCopyToPath);
destDir.setContextMenuId(Global.locked ? R.menu.menu_context_dir_locked : R.menu.menu_context_osdir);
destDir.setContextMenuId(LockScreen.isLocked(this.getActivity()) ? R.menu.menu_context_dir_locked : R.menu.menu_context_osdir);
destDir.show(getActivity().getFragmentManager(), "osdir");
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public class ImageDetailActivityViewPager extends LocalizedActivity implements C
private String mContextColumnExpression = null; // sql field expression. Result will be displayed in ImageView Context area
private String mContextName; // name of current ImageView Context persisted in bundle
private boolean mMustReplaceMenue = false;
private boolean locked = false; // if != Global.locked : must update menu

/** executes sql to load image detail data in a background task that may survive
* conriguration change (i.e. device rotation) */
Expand Down Expand Up @@ -536,6 +537,13 @@ protected void onActivityResult(final int requestCode,
final int resultCode, final Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);

final boolean locked = LockScreen.isLocked(this);
if (this.locked != locked) {
this.locked = locked;
mMustReplaceMenue = true;
invalidateOptionsMenu();
}

if (requestCode == ACTION_RESULT_FORWARD) {
// forward result from child-activity to parent-activity
setResult(resultCode, intent);
Expand Down Expand Up @@ -647,6 +655,14 @@ protected void onResume () {
unhideActionBar(Global.actionBarHideTimeInMilliSecs, "onResume");
Global.debugMemory(mDebugPrefix, "onResume");
super.onResume();

final boolean locked = LockScreen.isLocked(this);
if (this.locked != locked) {
this.locked = locked;
mMustReplaceMenue = true;
invalidateOptionsMenu();
}

if (Global.debugEnabledMemory) {
Log.d(Global.LOG_CONTEXT, mDebugPrefix + " - onResume cmd (" +
MoveOrCopyDestDirPicker.sFileCommands + ") => (" + mFileCommands +
Expand Down Expand Up @@ -812,8 +828,10 @@ public boolean onCreateOptionsMenu(Menu menu) {
}

private void defineMenu(Menu menu) {
if (Global.locked) {
if (LockScreen.isLocked(this)) {
getMenuInflater().inflate(R.menu.menu_image_detail_locked, menu);
LockScreen.fixMenu(menu);

} else {
getMenuInflater().inflate(R.menu.menu_image_detail, menu);
getMenuInflater().inflate(R.menu.menu_image_commands, menu);
Expand All @@ -826,7 +844,9 @@ private void defineMenu(Menu menu) {

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (mMustReplaceMenue) {
final boolean locked = LockScreen.isLocked(this);
if (mMustReplaceMenue || (locked != this.locked)) {
this.locked = locked;

mMustReplaceMenue = false;
menu.clear();
Expand Down Expand Up @@ -1034,7 +1054,7 @@ private boolean cmdMoveOrCopyWithDestDirPicker(final boolean move, String lastCo
destDir.defineDirectoryNavigation(OsUtils.getRootOSDirectory(),
(move) ? FotoSql.QUERY_TYPE_GROUP_MOVE : FotoSql.QUERY_TYPE_GROUP_COPY,
lastCopyToPath);
destDir.setContextMenuId(Global.locked ? R.menu.menu_context_dir_locked : R.menu.menu_context_osdir);
destDir.setContextMenuId(LockScreen.isLocked(this) ? R.menu.menu_context_dir_locked : R.menu.menu_context_osdir);
destDir.show(this.getFragmentManager(), "osdirimage");
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@

import de.k3b.android.androFotoFinder.FotoGalleryActivity;
import de.k3b.android.androFotoFinder.Global;
import de.k3b.android.androFotoFinder.LockScreen;
import de.k3b.android.androFotoFinder.R;
import de.k3b.android.androFotoFinder.ThumbNailUtils;
import de.k3b.android.androFotoFinder.imagedetail.ImageDetailActivityViewPager;
Expand Down Expand Up @@ -975,7 +976,7 @@ protected boolean showContextMenu(final View parent, final int markerId,
mTempPopupMenuParentView = OsmdroidUtil.openMapPopupView(mMapView, 0, new GeoPoint(geoPosition.getLatitude(), geoPosition.getLongitude()));
PopupMenu menu = new PopupMenu(getActivity(), mTempPopupMenuParentView);

inflater.inflate(Global.locked ? R.menu.menu_map_context_locked : R.menu.menu_map_context, menu.getMenu());
inflater.inflate(LockScreen.isLocked(this.getActivity()) ? R.menu.menu_map_context_locked : R.menu.menu_map_context, menu.getMenu());

menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(Global.locked ? R.menu.menu_map_context_locked : R.menu.menu_map_geo_picker, menu);
getMenuInflater().inflate(LockScreen.isLocked(this) ? R.menu.menu_map_context_locked : R.menu.menu_map_geo_picker, menu);
AboutDialogPreference.onPrepareOptionsMenu(this, menu);

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

import de.k3b.android.androFotoFinder.FotoGalleryActivity;
import de.k3b.android.androFotoFinder.Global;
import de.k3b.android.androFotoFinder.LockScreen;
import de.k3b.android.androFotoFinder.R;
import de.k3b.android.androFotoFinder.imagedetail.ImageDetailActivityViewPager;
import de.k3b.android.androFotoFinder.queries.FotoSql;
Expand Down Expand Up @@ -115,7 +116,7 @@ boolean onOk(List<String> addNames,
private AlertDialog mSubDialog = null;


private int mContextMenueId = Global.locked ? R.menu.menu_map_context_locked : R.menu.menu_tags_context;
private int mContextMenueId = LockScreen.isLocked(this.getActivity()) ? R.menu.menu_context_dir_locked : R.menu.menu_tags_context;
private int mTitleId = 0;

// local data
Expand Down Expand Up @@ -419,7 +420,7 @@ private void onShowPopUp(int contextMenueId, View anchor, Tag selection) {
// without mClipboardItem paste is not possible
if (mClipboardItem == null) {
MenuItem menuItem = popup.getMenu().findItem(android.R.id.paste);
menuItem.setVisible(false);
if (menuItem != null) menuItem.setVisible(false);
}

if (popup != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Date;

import de.k3b.android.androFotoFinder.Global;
import de.k3b.android.androFotoFinder.LockScreen;
import de.k3b.android.androFotoFinder.R;
import de.k3b.android.androFotoFinder.directory.DirectoryPickerFragment;
import de.k3b.android.androFotoFinder.media.AndroidJpgMetaWorkflow;
Expand Down Expand Up @@ -353,7 +354,7 @@ public void onPause() {
destDir.defineDirectoryNavigation(OsUtils.getRootOSDirectory(),
FotoSql.QUERY_TYPE_UNDEFINED,
getLastCopyToPath());
destDir.setContextMenuId(Global.locked ? R.menu.menu_context_dir_locked : R.menu.menu_context_osdir);
destDir.setContextMenuId(LockScreen.isLocked(mContext) ? R.menu.menu_context_dir_locked : R.menu.menu_context_osdir);
destDir.show(mContext.getFragmentManager(), "scannerPick");

return true;
Expand Down
Loading

0 comments on commit 142f550

Please sign in to comment.