1818
1919import android .annotation .SuppressLint ;
2020import android .app .Service ;
21- import android .content .Context ;
2221import android .content .Intent ;
2322import android .net .Uri ;
24- import android .net .wifi .WifiManager ;
2523import android .os .Binder ;
26- import android .os .Handler ;
2724import android .os .IBinder ;
28- import android .os .PowerManager ;
2925import android .util .Log ;
30- import android .widget .Toast ;
3126
3227import com .termux .terminal .EmulatorDebug ;
3328import com .termux .terminal .TerminalSession ;
@@ -42,30 +37,15 @@ public final class TermuxService extends Service implements SessionChangedCallba
4237 */
4338 public static final String ACTION_EXECUTE = "com.termux.service_execute" ;
4439
45-
4640 private static final String EXTRA_ARGUMENTS = "com.termux.execute.arguments" ;
4741 private static final String EXTRA_CURRENT_WORKING_DIRECTORY = "com.termux.execute.cwd" ;
48- private static final int NOTIFICATION_ID = 1337 ;
4942 private static final String ACTION_STOP_SERVICE = "com.termux.service_stop" ;
50- private static final String ACTION_LOCK_WAKE = "com.termux.service_wake_lock" ;
51- private static final String ACTION_UNLOCK_WAKE = "com.termux.service_wake_unlock" ;
5243 private static final String EXTRA_EXECUTE_IN_BACKGROUND = "com.termux.execute.background" ;
5344
5445
5546 private final IBinder mBinder = new LocalBinder ();
56- private final Handler mHandler = new Handler ();
5747 public SessionChangedCallback mSessionChangeCallback ;
58- /**
59- * If the user has executed the {@link #ACTION_STOP_SERVICE} intent.
60- */
61- private boolean mWantsToStop = false ;
6248 private TerminalSession mTerminalSessions = null ;
63- /**
64- * The wake lock and wifi lock are always acquired and released together.
65- */
66- private PowerManager .WakeLock mWakeLock ;
67- private WifiManager .WifiLock mWifiLock ;
68- private Toast toast ;
6949
7050 @ Override
7151 public void onCreate () {
@@ -77,30 +57,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
7757
7858 String action = intent .getAction ();
7959 if (ACTION_STOP_SERVICE .equals (action )) {
80- mWantsToStop = true ;
8160 mTerminalSessions .finishIfRunning ();
8261 stopSelf ();
83- } else if (ACTION_LOCK_WAKE .equals (action )) {
84- if (mWakeLock == null ) {
85- PowerManager pm = (PowerManager ) getSystemService (Context .POWER_SERVICE );
86- mWakeLock = pm .newWakeLock (PowerManager .PARTIAL_WAKE_LOCK , EmulatorDebug .LOG_TAG );
87- mWakeLock .acquire ();
88-
89- // http://tools.android.com/tech-docs/lint-in-studio-2-3#TOC-WifiManager-Leak
90- WifiManager wm = (WifiManager ) getApplicationContext ().getSystemService (Context .WIFI_SERVICE );
91- mWifiLock = wm .createWifiLock (WifiManager .WIFI_MODE_FULL_HIGH_PERF , EmulatorDebug .LOG_TAG );
92- mWifiLock .acquire ();
93-
94- }
95- } else if (ACTION_UNLOCK_WAKE .equals (action )) {
96- if (mWakeLock != null ) {
97- mWakeLock .release ();
98- mWakeLock = null ;
99-
100- mWifiLock .release ();
101- mWifiLock = null ;
102-
103- }
10462 } else if (ACTION_EXECUTE .equals (action )) {
10563 Uri executableUri = intent .getData ();
10664 String executablePath = (executableUri == null ? null : executableUri .getPath ());
@@ -147,17 +105,13 @@ public TerminalSession getTermSession() {
147105
148106 @ Override
149107 public void onDestroy () {
150- if (mWakeLock != null ) mWakeLock .release ();
151- if (mWifiLock != null ) mWifiLock .release ();
152-
153108 stopForeground (true );
154109 //System.out.println("TermuxService:onDestroy");
155110 mTerminalSessions .finishIfRunning ();
156111
157112 }
158113
159114 private TerminalSession createTermSession (String executablePath , String [] arguments , String cwd ) {
160- //System.out.println("----------------->createTermSession");
161115 File f = new File (EnvironmentVariable .getHomePath (this ));
162116 if (!f .exists ())
163117 f .mkdirs ();
@@ -195,12 +149,7 @@ private TerminalSession createTermSession(String executablePath, String[] argume
195149 if (processArgs .length > 1 )
196150 System .arraycopy (processArgs , 1 , args , 1 , processArgs .length - 1 );
197151
198- TerminalSession session = new TerminalSession (executablePath , cwd , args , env , this );
199- return session ;
200- }
201-
202- public void removeTermSession () {
203- stopSelf ();
152+ return new TerminalSession (executablePath , cwd , args , env , this );
204153 }
205154
206155 @ Override
@@ -238,15 +187,6 @@ public void onBackgroundJobExited(final BackgroundJob task) {
238187 stopSelf ();
239188 }
240189
241- private void showToast (CharSequence text ) {
242- if (toast == null ) {
243- toast = Toast .makeText (this , text , Toast .LENGTH_SHORT );
244- } else {
245- toast .setText (text );
246- }
247- toast .show ();
248- }
249-
250190 /**
251191 * This service is only bound from inside the same process and never uses IPC.
252192 */
0 commit comments