Repository navigation
Expand file tree
/
Copy pathMainActivity.java
More file actions
315 lines (286 loc) · 12.9 KB
/
Copy pathMainActivity.java
File metadata and controls
315 lines (286 loc) · 12.9 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package ru.meefik.linuxdeploy;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.text.method.LinkMovementMethod;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
public class MainActivity extends SherlockActivity {
private static TextView output;
private static ScrollView scroll;
private static WifiLock wifiLock;
final static int NOTIFY_ID = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PrefStore.setLocale(this);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
output = (TextView) findViewById(R.id.LogView);
scroll = (ScrollView) findViewById(R.id.LogScrollView);
output.setMovementMethod(LinkMovementMethod.getInstance());
// WiFi lock init
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL,
"linuxdeploy");
}
@Override
public void setTheme(int resid) {
super.setTheme(PrefStore.getTheme(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
PrefStore.setLocale(this);
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
boolean isLight = PrefStore.getTheme(this) == R.style.LightTheme;
menu.findItem(R.id.menu_properties).setIcon(
isLight ? R.drawable.ic_action_properties_light
: R.drawable.ic_action_properties_dark);
int ot = getResources().getConfiguration().orientation;
if (ot == Configuration.ORIENTATION_LANDSCAPE) {
menu.findItem(R.id.menu_start).setIcon(
isLight ? R.drawable.ic_action_start_light
: R.drawable.ic_action_start_dark);
menu.findItem(R.id.menu_stop).setIcon(
isLight ? R.drawable.ic_action_stop_light
: R.drawable.ic_action_stop_dark);
}
super.onCreateOptionsMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_start:
new AlertDialog.Builder(this)
.setTitle(R.string.confirm_start_title).setMessage(
R.string.confirm_start_message).setIcon(
android.R.drawable.ic_dialog_alert).setCancelable(
false).setPositiveButton(android.R.string.yes,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
new ExecScript(getApplicationContext(),
"start").start();
// actions
Handler h = new Handler();
if (PrefStore.getStartup(
getApplicationContext()).contains(
"xserver")
&& PrefStore
.isXserverXsdl(getApplicationContext())) {
h.postDelayed(new Runnable() {
public void run() {
PackageManager pm = getPackageManager();
Intent intent = pm
.getLaunchIntentForPackage("x.org.server");
if (intent != null)
startActivity(intent);
}
}, 1000);
}
if (PrefStore.getStartup(
getApplicationContext()).contains(
"framebuffer")) {
h.postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(
getApplicationContext(),
FullscreenActivity.class);
if (intent != null)
startActivity(intent);
}
}, 1000);
}
}
}).setNegativeButton(android.R.string.no,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
}).show();
break;
case R.id.menu_stop:
new AlertDialog.Builder(this).setTitle(R.string.confirm_stop_title)
.setMessage(R.string.confirm_stop_message).setIcon(
android.R.drawable.ic_dialog_alert).setCancelable(
false).setPositiveButton(android.R.string.yes,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
new ExecScript(getApplicationContext(),
"stop").start();
}
}).setNegativeButton(android.R.string.no,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
}).show();
break;
case R.id.menu_status:
new ExecScript(getApplicationContext(), "status").start();
break;
case R.id.menu_terminal:
try {
Intent intent_terminal = new Intent("jackpal.androidterm.RUN_SCRIPT");
intent_terminal.addCategory(Intent.CATEGORY_DEFAULT);
intent_terminal.putExtra("jackpal.androidterm.iInitialCommand",
PrefStore.getTerminalCmd(this));
startActivity(intent_terminal);
} catch(Exception e) {
Toast.makeText(getApplicationContext(),
R.string.toast_terminal_error, Toast.LENGTH_SHORT).show();
}
break;
case R.id.menu_properties:
Intent intent_properties = new Intent(this,
PropertiesActivity.class);
startActivity(intent_properties);
break;
case R.id.menu_settings:
Intent intent_settings = new Intent(this, SettingsActivity.class);
startActivity(intent_settings);
break;
case R.id.menu_about:
Intent intent_about = new Intent(this, AboutActivity.class);
startActivity(intent_about);
break;
case R.id.menu_clear:
clearLog();
break;
case R.id.menu_exit:
notification(getApplicationContext());
if (wifiLock.isHeld()) wifiLock.release();
finish();
break;
case android.R.id.home:
Intent intent_profiles = new Intent(this, ProfilesActivity.class);
startActivity(intent_profiles);
break;
default:
return super.onOptionsItemSelected(item);
}
return false;
}
@Override
public void onResume() {
super.onResume();
String profileName = PrefStore
.getCurrentProfileTitle(getApplicationContext());
String ipaddress = PrefStore.getLocalIpAddress();
setTitle(profileName + " [ " + ipaddress + " ]");
// show icon
notification(getApplicationContext(), getIntent());
// Restore font size
output.setTextSize(TypedValue.COMPLEX_UNIT_SP, PrefStore
.getFontSize(this));
// Restore log
if (Logger.get().length() == 0) {
output.setText(R.string.help_text);
} else {
showLog(Logger.get());
}
// Screen lock
if (PrefStore.isScreenLock(this))
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
else getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// WiFi lock
if (PrefStore.isWifiLock(this))
wifiLock.acquire();
else if (wifiLock.isHeld()) wifiLock.release();
// update configuration file
if (PrefStore.CONF_CHANGE) {
PrefStore.CONF_CHANGE = false;
// update config file
EnvUtils.updateConf(this);
}
}
private static void clearLog() {
Logger.clear();
output.setText(R.string.help_text);
}
/**
* Show message in TextView, used from Logger
*
* @param log message
*/
public static void showLog(final String log) {
if (output == null || scroll == null) return;
// show log in TextView
output.post(new Runnable() {
@Override
public void run() {
output.setText(log);
// scroll TextView to bottom
scroll.post(new Runnable() {
@Override
public void run() {
scroll.fullScroll(View.FOCUS_DOWN);
scroll.clearFocus();
}
});
}
});
}
public static void notification(Context context, Intent intent) {
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
if (PrefStore.isShowIcon(context)) {
PrefStore.setLocale(context);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(
context.getString(R.string.notification_current_profile)
+ ": "
+ PrefStore.getCurrentProfileTitle(context));
Intent resultIntent = intent;
if (resultIntent == null)
resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setOngoing(true);
mBuilder.setWhen(0);
mNotificationManager.notify(NOTIFY_ID, mBuilder.build());
} else {
mNotificationManager.cancel(NOTIFY_ID);
}
}
public static void notification(Context context) {
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(NOTIFY_ID);
}
}