Created
October 10, 2014 09:53
-
-
Save ov3rk1ll/467db75bd559f7f97f9e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.handheldgroup.fullfocus; | |
| import android.app.Activity; | |
| import android.app.ActivityManager; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.os.Bundle; | |
| import android.view.KeyEvent; | |
| import java.lang.reflect.Method; | |
| /* | |
| This Activity prevents the user form exiting the Activity as well as preventing access to the OS itself | |
| - onPause is overridden to prevent other apps from gaining focus on top of this app | |
| - onWindowFocusChanged is overridden to bring focus back to this app if its lost to recent or other system windows | |
| - onWindowFocusChanged is also used to detect if the StatusBar is opened an forces it back to collapse | |
| - onKeyDown is overridden to prevent the BACK-Key from triggering the app to close | |
| */ | |
| public class FullFocusActivity extends Activity { | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_my); | |
| } | |
| @Override | |
| public void onPause() { | |
| super.onPause(); | |
| // Prevent a different Activity (from different package) to come to front | |
| // Eg. the users tries to open the option on a keyboard | |
| ActivityManager activityManager = | |
| (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); | |
| activityManager.moveTaskToFront(getTaskId(), 0); | |
| } | |
| public void onWindowFocusChanged(boolean hasFocus) { | |
| if (!hasFocus) { | |
| // Close the recent apps and power dialog | |
| Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); | |
| sendBroadcast(closeDialog); | |
| // Collapse the StatusBar (NotificationBar) when pulled down | |
| // NOTE: Requires "<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>" | |
| // to be added to the apps manifest xml file (outside the application tag) | |
| try { | |
| Object service = getSystemService("statusbar"); | |
| Class<?> statusbarManager = Class.forName("android.app.StatusBarManager"); | |
| Method collapse = statusbarManager.getMethod("collapse"); | |
| collapse.setAccessible(true); | |
| collapse.invoke(service); | |
| } catch (Exception ex) { | |
| } | |
| } | |
| } | |
| @Override | |
| public boolean onKeyDown(int keyCode, KeyEvent event) { | |
| // Consume the BACK-Key to the user can't exit the app | |
| // NOTE: This should only be used on the "Main Activity" or will require a more complex code | |
| // to allow normal navigation within the app itself | |
| if(keyCode == KeyEvent.KEYCODE_BACK ){ | |
| return true; | |
| } | |
| return super.onKeyDown(keyCode, event); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment