Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CallRecordService extends Service {
- CallInfoPreferenceManager preferenceManager;
- private static final String USER_AGENT = "UploadServiceDemo/" + BuildConfig.VERSION_NAME;
- private ArrayList<File> fileList = new ArrayList<File>();
- private ArrayList<File> fileListWasAdded = new ArrayList<File>();
- AudioRecorder recorder;
- CallDAO dao;
- Context context;
- HistoryFragment historyFragment;
- private File pathRootLapsoft;
- ListView listView;
- HistoryAdapter historyAdapter;
- ArrayList<Call> callList;
- @Override
- public void onCreate() {
- super.onCreate();
- context = this;
- preferenceManager = CallInfoPreferenceManager.getInstance();
- recorder = new AudioRecorder(preferenceManager.getStartDate(), preferenceManager.getPhoneNumber());
- historyFragment = new HistoryFragment();
- dao = new CallDAO(context);
- Cursor cursor = dao.select();
- callList = dao.cursorCallList(cursor);
- historyAdapter = new HistoryAdapter(context, R.layout.call_history_item, callList);
- listView = new ListView(context);
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Log.d("service", "onStartCommand");
- Thread thread = new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- recorder.start();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- thread.start();
- return super.onStartCommand(intent, flags, startId);
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onDestroy() {
- Log.d("service", "destroy");
- Thread thread = new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- recorder.stop();
- MultipartUploadForRecordApp();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- thread.start();
- super.onDestroy();
- }
- private void showToast(String message) {
- Toast.makeText(this, message, Toast.LENGTH_LONG).show();
- }
- private List<File> getListFiles(File parentDir) {
- ArrayList<File> inFiles = new ArrayList<File>();
- File[] files = parentDir.listFiles();
- for (File file : files) {
- if (file.isFile()) {
- inFiles.addAll(getListFiles(file));
- } else {
- if(file.getName().endsWith(".csv")){
- inFiles.add(file);
- }
- }
- }
- return inFiles;
- }
- public ArrayList<File> getFileFromDirectory(File dir) {
- File listFile[] = dir.listFiles();
- if (listFile != null && listFile.length > 0) {
- for (int i = 0; i < listFile.length; i++) {
- if (listFile[i].isDirectory()) {
- fileList.add(listFile[i]);
- getFileFromDirectory(listFile[i]);
- } else {
- if (listFile[i].getName().endsWith(".mp3")
- || listFile[i].getName().endsWith(".3gp"))
- {
- fileList.add(listFile[i]);
- }
- }
- }
- }
- return fileList;
- }
- SparseBooleanArray selected;
- public void MultipartUploadForRecordApp() {
- final String serverUrlString = "http://www.example.com/main.php&tp=0";
- final String paramNameString = "uploaded_file";
- pathRootLapsoft = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/LapsoftCRM/");
- fileListWasAdded = getFileFromDirectory(pathRootLapsoft);
- int position = 0;
- if(fileListWasAdded.isEmpty())
- listView.setAdapter(null);
- for(File filePathName : fileListWasAdded)
- try {
- String filename = getFilename(filePathName.getPath());
- MultipartUploadRequest req = new MultipartUploadRequest(this, serverUrlString)
- .addFileToUpload(filePathName.getPath(), paramNameString)
- .setCustomUserAgent(USER_AGENT)
- .setAutoDeleteFilesAfterSuccessfulUpload(true)
- .setUsesFixedLengthStreamingMode(true)
- .setMaxRetries(2);
- dao.deleteCallWhenUploadSuccess(filename);
- historyAdapter.arraySrc.remove(position);
- historyAdapter.notifyDataSetChanged();
- listView.setAdapter(historyAdapter);
- String uploadID = req.startUpload();
- position++;
- } catch (FileNotFoundException exc) {
- showToast(exc.getMessage());
- } catch (IllegalArgumentException exc) {
- showToast("Missing some arguments. " + exc.getMessage());
- }
- catch (MalformedURLException exc) {
- showToast(exc.getMessage());
- }
- }
- private String getFilename(String filepath) {
- if (filepath == null)
- return null;
- final String[] filepathParts = filepath.split("/");
- return filepathParts[filepathParts.length - 1];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement