forked from uberspot/2048-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
169 lines (148 loc) · 5.73 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.uberspot.a2048;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
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.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView mWebView;
private long mLastBackPress;
private static final long mBackPressThreshold = 3500;
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;
private Toast pressBackToast;
@SuppressLint({ "SetJavaScriptEnabled", "NewApi", "ShowToast" })
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Don't show an action bar or title
requestWindowFeature(Window.FEATURE_NO_TITLE);
// If on android 3.0+ activate hardware acceleration
if (Build.VERSION.SDK_INT >= 11){
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
}
// Apply previous setting about showing status bar or not
applyFullScreen(isFullScreen());
// Check if screen rotation is locked in settings
boolean isOrientationEnabled = false;
try {
isOrientationEnabled = Settings.System.getInt(getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION) == 1;
} catch (SettingNotFoundException e) { }
// If rotation isn't locked and it's a LARGE screen then add orientation changes based on sensor
int screenLayout = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
if ( (screenLayout == Configuration.SCREENLAYOUT_SIZE_LARGE
|| screenLayout == Configuration.SCREENLAYOUT_SIZE_XLARGE )
&& isOrientationEnabled) {
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
setContentView(R.layout.activity_main);
// Load webview with game
mWebView = (WebView) findViewById(R.id.mainWebView);
WebSettings settings = mWebView.getSettings();
String packageName = getPackageName();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
settings.setRenderPriority(RenderPriority.HIGH);
settings.setDatabasePath("/data/data/" + packageName + "/databases");
// If there is a previous instance restore it in the webview
if (savedInstanceState != null) {
mWebView.restoreState(savedInstanceState);
} else {
mWebView.loadUrl("file:///android_asset/2048/index.html");
}
Toast.makeText(getApplication(),
R.string.toggle_fullscreen, Toast.LENGTH_SHORT).show();
// Set fullscreen toggle on webview LongClick
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_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;
}});
pressBackToast = Toast.makeText(getApplicationContext(),
R.string.press_back_again_to_exit, Toast.LENGTH_SHORT);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
mWebView.saveState(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);
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);
}
/**
* Toggles the activitys fullscreen mode by setting the corresponding window flag
* @param isFullScreen
*/
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 onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public void onBackPressed() {
long currentTime = System.currentTimeMillis();
if (Math.abs(currentTime - mLastBackPress) > mBackPressThreshold) {
pressBackToast.show();
mLastBackPress = currentTime;
} else {
pressBackToast.cancel();
super.onBackPressed();
}
}
}