-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellPhone.java
More file actions
198 lines (182 loc) · 5.08 KB
/
ShellPhone.java
File metadata and controls
198 lines (182 loc) · 5.08 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
package com.shellphone.android;
import android.app.Service;
import android.os.IBinder;
import android.net.Uri;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import java.net.ServerSocket;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Handler;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.InetAddress;
public class ShellPhone extends Service {
private ServerSocket serverSocket;
Thread serverThread = null;
public static final int SERVERPORT = 6000;
private Handler handler = new Handler();
String line = null;
@Override
public void onCreate() {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
MyPhoneListener phoneListener = new MyPhoneListener();
TelephonyManager telephonyManager =
(TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
// receive notifications of telephony state changes
telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
// this.serverThread = new Thread(new ServerThread());
this.serverThread = new Thread(new LocalServerThread());
this.serverThread.start();
}
private String call(String number) {
try {
// set the data
String uri = "tel:"+number;
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(callIntent);
return("Done");
}catch(Exception e) {
// Toast.makeText(getApplicationContext(),"Your call has failed...",
// Toast.LENGTH_LONG).show();
e.printStackTrace();
return(number + " Your call has failed...");
}
}
private String dial(String number) {
try {
String uri = "tel:"+number;
Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse(uri));
startActivity(dialIntent);
return("Done");
}catch(Exception e) {
// Toast.makeText(getApplicationContext(),"Your call has failed...",
// Toast.LENGTH_LONG).show();
e.printStackTrace();
return(number + " Your call has failed...");
}
}
private class MyPhoneListener extends PhoneStateListener {
private boolean onCall = false;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// phone ringing...
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// one call exists that is dialing, active, or on hold
onCall = true;
break;
case TelephonyManager.CALL_STATE_IDLE:
// in initialization of the class and at the end of phone call
// detect flag from CALL_STATE_OFFHOOK
if (onCall == true) {
onCall = false;
}
break;
default:
break;
}
}
}
private class ServerThread implements Runnable {
public void run() {
Socket server = null;
try {
serverSocket = new ServerSocket(SERVERPORT);
} catch (IOException e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {
try {
server = serverSocket.accept();
// connected
handler.post(new Runnable() {
@Override
public void run() {
// on connected
}
});
try {
BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
// BufferedWriter out = new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));
// String line = null;
while ((line = in.readLine()) != null) {
// Log.d("ServerActivity", line);
handler.post(new Runnable() {
@Override
public void run() {
call(line);
// update
}
});
}
break;
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
// interrupted connection
}
});
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}
@Override
public void onDestroy() {
}
private class LocalServerThread implements Runnable{
// AF_LOCAL/UNIX
LocalSocket ls = null;
LocalSocketAddress lsa = null;
public void run() {
ls = new LocalSocket();
lsa = new LocalSocketAddress("/data/local/tmp/ShellPhoneSocket");
try {
ls.bind(lsa);
ls.connect(lsa);
} catch (IOException e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(ls.getInputStream()));
while ((line = in.readLine()) != null) {
handler.post(new Runnable() {
@Override
public void run() {
call(line);
// update
}
});
}
break;
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
// interrupted connection
}
});
e.printStackTrace();
}
}
}
}
}