Skip to content

Commit

Permalink
make fullscreen toggleable with long touch
Browse files Browse the repository at this point in the history
  • Loading branch information
uberspot committed Mar 28, 2014
1 parent 501229e commit 9fcdfb7
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
Binary file modified 2048.apk
Binary file not shown.
5 changes: 2 additions & 3 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.uberspot.a2048"
android:versionCode="7"
android:versionName="1.6" >
android:versionCode="8"
android:versionName="1.7" >

<uses-sdk
android:minSdkVersion="10"
Expand All @@ -15,7 +15,6 @@
android:theme="@style/AppTheme" >
<activity
android:name="com.uberspot.a2048.MainActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name" >
Expand Down
Binary file modified libs/android-support-v4.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<string name="app_name">2048</string>
<string name="action_settings">Settings</string>
<string name="press_back_again_to_exit">Press back again to exit</string>

<string name="toggle_fullscreen">Long click to toggle fullscreen</string>

</resources>
70 changes: 65 additions & 5 deletions src/com/uberspot/a2048/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,44 @@
import com.uberspot.a2048.R;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

public class MainActivity extends Activity {

private WebView mWebView;
private long mLastPress;
private long mLastBackPress;
private static final long mBackPressThreshold = 4000;
private static final String IS_FULLSCREEN_PREF = "is_fullscreen_pref";
private static boolean DEF_FULLSCREEN = true;
private long mLastTouch;
private static final long mTouchThreshold = 2000;

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
applyFullScreen(isFullScreen());

setContentView(R.layout.activity_main);

mWebView = (WebView) findViewById(R.id.mainWebView);
WebSettings settings = mWebView.getSettings();
String packageName = "com.uberspot.a2048";
String packageName = getPackageName();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
Expand All @@ -34,6 +51,28 @@ protected void onCreate(Bundle savedInstanceState) {
} else {
mWebView.loadUrl("file:///android_asset/2048/index.html");
}
Toast.makeText(getApplication(),
R.string.toggle_fullscreen, Toast.LENGTH_SHORT).show();
mWebView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// Implement a long touch action by comparing
// time between action up and action down
long currentTime = System.currentTimeMillis();
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return false;
} else if(event.getAction() == MotionEvent.ACTION_UP &&
Math.abs(currentTime - mLastTouch) > mTouchThreshold) {
boolean toggledFullScreen = !isFullScreen();
saveFullScreen(toggledFullScreen);
applyFullScreen(toggledFullScreen);
} else if(event.getAction() == MotionEvent.ACTION_DOWN) {
mLastTouch = currentTime;
}
// return so that the event isn't consumed but used
// by the webview as well
return false;
}});
}

@Override
Expand All @@ -44,19 +83,40 @@ protected void onSaveInstanceState(Bundle outState) {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
//getMenuInflater().inflate(R.menu.main, menu);
return true;
}

private void saveFullScreen(boolean isFullScreen) {
// save in preferences
SharedPreferences.Editor editor = PreferenceManager
.getDefaultSharedPreferences(this).edit();
editor.putBoolean(IS_FULLSCREEN_PREF, isFullScreen);
editor.commit();
}

private boolean isFullScreen() {
return PreferenceManager
.getDefaultSharedPreferences(this)
.getBoolean(IS_FULLSCREEN_PREF, DEF_FULLSCREEN);
}

private void applyFullScreen(boolean isFullScreen) {
if(isFullScreen) {
getWindow().clearFlags(LayoutParams.FLAG_FULLSCREEN);
} else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
@Override
public void onBackPressed() {
long currentTime = System.currentTimeMillis();
Toast pressBackToast = Toast.makeText(getApplicationContext(),
R.string.press_back_again_to_exit, Toast.LENGTH_SHORT);
if (Math.abs(currentTime - mLastPress) > mBackPressThreshold) {
if (Math.abs(currentTime - mLastBackPress) > mBackPressThreshold) {
pressBackToast.show();
mLastPress = currentTime;
mLastBackPress = currentTime;
} else {
pressBackToast.cancel();
super.onBackPressed();
Expand Down

0 comments on commit 9fcdfb7

Please sign in to comment.