Repository: eventfahrplan/eventfahrplan
Issue: Restrict usage while screen is locked

# Environment
- App version: [v.1.38.4](https://github.com/EventFahrplan/EventFahrplan/releases/tag/v.1.38.4)

# How to reproduce
1. Launch the app.
2. Lock the device.
3. Use the app on the **lock screen**.

# Observed behavior
- **All screens** of the app can be used.

# Expected behavior
- Only the **schedule view** and the **event details** view can be visited.
- Lock screen users are **not** be able to visit the **app settings** where they can change the schedule URL.
- Lock screen users are **not** be able to visit the **alarms list** where all alarms can be deleted.
- Lock screen users are **not** be able to visit the **favorites list** where all alarms can be deleted.

# Devices
- Please check both phones and tablets as the screens are set up differently.

# Related
- This feature has been added in 1be512c101c6d4df07a74269c41856289c968229 and released with [v.1.38.4](https://github.com/EventFahrplan/EventFahrplan/releases/tag/v.1.38.4).


Changed files:
app/src/main/java/nerd/tuxmobil/fahrplan/congress/schedule/MainActivity.java, app/src/main/java/nerd/tuxmobil/fahrplan/congress/base/BaseActivity.java, app/src/main/java/nerd/tuxmobil/fahrplan/congress/details/EventDetail.java

diff --git a/app/src/main/java/nerd/tuxmobil/fahrplan/congress/base/BaseActivity.java b/app/src/main/java/nerd/tuxmobil/fahrplan/congress/base/BaseActivity.java
index 34be9483..d602d365 100644
--- a/app/src/main/java/nerd/tuxmobil/fahrplan/congress/base/BaseActivity.java
+++ b/app/src/main/java/nerd/tuxmobil/fahrplan/congress/base/BaseActivity.java
@@ -11,7 +11,6 @@ import android.support.v4.app.FragmentManager;
 import android.support.v7.app.AppCompatActivity;
 import android.util.Log;
 import android.view.MenuItem;
-import android.view.WindowManager;
 
 import nerd.tuxmobil.fahrplan.congress.R;
 import nerd.tuxmobil.fahrplan.congress.autoupdate.UpdateService;
@@ -22,12 +21,6 @@ public abstract class BaseActivity extends AppCompatActivity {
 
     private ConnectivityObserver connectivityObserver;
 
-    @Override
-    public void onAttachedToWindow() {
-        super.onAttachedToWindow();
-        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
-    }
-
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
diff --git a/app/src/main/java/nerd/tuxmobil/fahrplan/congress/details/EventDetail.java b/app/src/main/java/nerd/tuxmobil/fahrplan/congress/details/EventDetail.java
index 3a8e63d0..ded55ac7 100644
--- a/app/src/main/java/nerd/tuxmobil/fahrplan/congress/details/EventDetail.java
+++ b/app/src/main/java/nerd/tuxmobil/fahrplan/congress/details/EventDetail.java
@@ -9,6 +9,7 @@ import android.support.annotation.NonNull;
 import android.support.v4.content.ContextCompat;
 import android.view.Menu;
 import android.view.MenuItem;
+import android.view.WindowManager;
 
 import nerd.tuxmobil.fahrplan.congress.BuildConfig;
 import nerd.tuxmobil.fahrplan.congress.MyApp;
@@ -39,6 +40,12 @@ public class EventDetail extends BaseActivity {
         activity.startActivityForResult(intent, MyApp.EVENTVIEW);
     }
 
+    @Override
+    public void onAttachedToWindow() {
+        super.onAttachedToWindow();
+        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
+    }
+
     @Override
     protected void onCreate(Bundle arg0) {
         super.onCreate(arg0);
diff --git a/app/src/main/java/nerd/tuxmobil/fahrplan/congress/schedule/MainActivity.java b/app/src/main/java/nerd/tuxmobil/fahrplan/congress/schedule/MainActivity.java
index f5768813..9fd030de 100644
--- a/app/src/main/java/nerd/tuxmobil/fahrplan/congress/schedule/MainActivity.java
+++ b/app/src/main/java/nerd/tuxmobil/fahrplan/congress/schedule/MainActivity.java
@@ -1,11 +1,13 @@
 package nerd.tuxmobil.fahrplan.congress.schedule;
 
 import android.app.Activity;
+import android.app.KeyguardManager;
 import android.app.ProgressDialog;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.graphics.drawable.ColorDrawable;
+import android.os.Build;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
 import android.support.annotation.IdRes;
@@ -23,6 +25,7 @@ import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
+import android.view.WindowManager;
 import android.widget.FrameLayout;
 import android.widget.ProgressBar;
 
@@ -73,12 +76,22 @@ public class MainActivity extends BaseActivity implements
 
     private ProgressDialog progress = null;
 
+    private KeyguardManager keyguardManager = null;
+
     private ProgressBar progressBar = null;
     private boolean requiresScheduleReload = false;
     private boolean shouldScrollToCurrent = true;
     private boolean showUpdateAction = true;
+    private boolean isScreenLocked = false;
+    private boolean isFavoritesInSidePane = false;
     private static MainActivity instance;
 
+    @Override
+    public void onAttachedToWindow() {
+        super.onAttachedToWindow();
+        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
+    }
+
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -87,6 +100,7 @@ public class MainActivity extends BaseActivity implements
 
         MyApp.LogDebug(LOG_TAG, "onCreate");
         setContentView(R.layout.main_layout);
+        keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
         Toolbar toolbar = findViewById(R.id.toolbar);
         progressBar = findViewById(R.id.progress);
         setSupportActionBar(toolbar);
@@ -264,6 +278,15 @@ public class MainActivity extends BaseActivity implements
     @Override
     protected void onResume() {
         super.onResume();
+        isScreenLocked = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
+                ? keyguardManager.isKeyguardLocked()
+                : keyguardManager.inKeyguardRestrictedInputMode();
+
+        FrameLayout sidePane = findViewById(R.id.detail);
+        if (sidePane != null && isFavoritesInSidePane){
+            sidePane.setVisibility(isScreenLocked ? View.GONE : View.VISIBLE);
+        }
+
         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
         if (prefs.getBoolean(BundleKeys.PREFS_CHANGES_SEEN, true) == false) {
             showChangesDialog();
@@ -376,6 +399,9 @@ public class MainActivity extends BaseActivity implements
         if (sidePane != null) {
             sidePane.setVisibility(View.GONE);
         }
+        if (fragmentTag.equals(StarredListFragment.FRAGMENT_TAG)) {
+            isFavoritesInSidePane = false;
+        }
         removeFragment(fragmentTag);
     }
 
@@ -464,7 +490,9 @@ public class MainActivity extends BaseActivity implements
         boolean found = fragment != null;
         View sidePane = findViewById(detailView);
         if (sidePane != null) {
-            sidePane.setVisibility(found ? View.VISIBLE : View.GONE);
+            isFavoritesInSidePane = found && fragment instanceof StarredListFragment;
+            sidePane.setVisibility(isFavoritesInSidePane && isScreenLocked || !found
+                    ? View.GONE : View.VISIBLE);
         }
     }
 
@@ -481,8 +509,10 @@ public class MainActivity extends BaseActivity implements
         if (sidePane == null) {
             Intent intent = new Intent(this, StarredListActivity.class);
             startActivityForResult(intent, MyApp.STARRED);
-        } else {
+        }
+        else if(!isScreenLocked) {
             sidePane.setVisibility(View.VISIBLE);
+            isFavoritesInSidePane = true;
             replaceFragment(R.id.detail, StarredListFragment.newInstance(true),
                     StarredListFragment.FRAGMENT_TAG, StarredListFragment.FRAGMENT_TAG);
         }