Advertisement
Guest User

Untitled

a guest
Mar 17th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. public class CallRecordService extends Service {
  2.  
  3.     CallInfoPreferenceManager preferenceManager;
  4.     private static final String USER_AGENT = "UploadServiceDemo/" + BuildConfig.VERSION_NAME;
  5.     private ArrayList<File> fileList = new ArrayList<File>();
  6.     private ArrayList<File> fileListWasAdded = new ArrayList<File>();
  7.     AudioRecorder recorder;
  8.     CallDAO dao;
  9.     Context context;
  10.     HistoryFragment historyFragment;
  11.     private File pathRootLapsoft;
  12.  
  13.     ListView listView;
  14.     HistoryAdapter historyAdapter;
  15.     ArrayList<Call> callList;
  16.     @Override
  17.     public void onCreate() {
  18.         super.onCreate();
  19.         context = this;
  20.         preferenceManager = CallInfoPreferenceManager.getInstance();
  21.         recorder = new AudioRecorder(preferenceManager.getStartDate(), preferenceManager.getPhoneNumber());
  22.         historyFragment = new HistoryFragment();
  23.         dao = new CallDAO(context);
  24.         Cursor cursor = dao.select();
  25.         callList = dao.cursorCallList(cursor);
  26.         historyAdapter = new HistoryAdapter(context, R.layout.call_history_item, callList);
  27.         listView = new ListView(context);
  28.     }
  29.  
  30.     @Override
  31.     public int onStartCommand(Intent intent, int flags, int startId) {
  32.         Log.d("service", "onStartCommand");
  33.         Thread thread = new Thread(new Runnable() {
  34.             @Override
  35.             public void run() {
  36.                 try {
  37.                     recorder.start();
  38.                 } catch (Exception e) {
  39.                     // TODO Auto-generated catch block
  40.                     e.printStackTrace();
  41.                 }
  42.             }
  43.         });
  44.         thread.start();
  45.         return super.onStartCommand(intent, flags, startId);
  46.     }
  47.  
  48.     @Override
  49.     public IBinder onBind(Intent intent) {
  50.         return null;
  51.     }
  52.  
  53.     @Override
  54.     public void onDestroy() {
  55.         Log.d("service", "destroy");
  56.         Thread thread = new Thread(new Runnable() {
  57.             @Override
  58.             public void run() {
  59.                 try {
  60.                     recorder.stop();
  61.                     MultipartUploadForRecordApp();
  62.                 } catch (Exception e) {
  63.                     // TODO Auto-generated catch block
  64.                     e.printStackTrace();
  65.                 }
  66.             }
  67.         });
  68.         thread.start();
  69.         super.onDestroy();
  70.     }
  71.  
  72.  
  73.     private void showToast(String message) {
  74.         Toast.makeText(this, message, Toast.LENGTH_LONG).show();
  75.     }
  76.  
  77.     private List<File> getListFiles(File parentDir) {
  78.         ArrayList<File> inFiles = new ArrayList<File>();
  79.         File[] files = parentDir.listFiles();
  80.         for (File file : files) {
  81.             if (file.isFile()) {
  82.                 inFiles.addAll(getListFiles(file));
  83.             } else {
  84.                 if(file.getName().endsWith(".csv")){
  85.                     inFiles.add(file);
  86.                 }
  87.             }
  88.         }
  89.         return inFiles;
  90.     }
  91.  
  92.  
  93.     public ArrayList<File> getFileFromDirectory(File dir) {
  94.         File listFile[] = dir.listFiles();
  95.         if (listFile != null && listFile.length > 0) {
  96.             for (int i = 0; i < listFile.length; i++) {
  97.  
  98.                 if (listFile[i].isDirectory()) {
  99.                     fileList.add(listFile[i]);
  100.                     getFileFromDirectory(listFile[i]);
  101.  
  102.                 } else {
  103.                     if (listFile[i].getName().endsWith(".mp3")
  104.                             || listFile[i].getName().endsWith(".3gp"))
  105.  
  106.                     {
  107.                         fileList.add(listFile[i]);
  108.                     }
  109.                 }
  110.  
  111.             }
  112.         }
  113.         return fileList;
  114.     }
  115.  
  116.     SparseBooleanArray selected;
  117.     public void MultipartUploadForRecordApp() {
  118.         final String serverUrlString = "http://www.example.com/main.php&tp=0";
  119.         final String paramNameString = "uploaded_file";
  120.  
  121.         pathRootLapsoft = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/LapsoftCRM/");
  122.         fileListWasAdded = getFileFromDirectory(pathRootLapsoft);
  123.         int position = 0;
  124.         if(fileListWasAdded.isEmpty())
  125.             listView.setAdapter(null);
  126.         for(File filePathName : fileListWasAdded)
  127.         try {
  128.             String filename = getFilename(filePathName.getPath());
  129.             MultipartUploadRequest req = new MultipartUploadRequest(this, serverUrlString)
  130.                     .addFileToUpload(filePathName.getPath(), paramNameString)
  131.                     .setCustomUserAgent(USER_AGENT)
  132.                     .setAutoDeleteFilesAfterSuccessfulUpload(true)
  133.                     .setUsesFixedLengthStreamingMode(true)
  134.                     .setMaxRetries(2);
  135.             dao.deleteCallWhenUploadSuccess(filename);
  136.              historyAdapter.arraySrc.remove(position);
  137.             historyAdapter.notifyDataSetChanged();
  138.             listView.setAdapter(historyAdapter);
  139.             String uploadID = req.startUpload();
  140.             position++;
  141.  
  142.         } catch (FileNotFoundException exc) {
  143.             showToast(exc.getMessage());
  144.         } catch (IllegalArgumentException exc) {
  145.             showToast("Missing some arguments. " + exc.getMessage());
  146.         }
  147.         catch (MalformedURLException exc) {
  148.             showToast(exc.getMessage());
  149.         }
  150.     }
  151.  
  152.     private String getFilename(String filepath) {
  153.         if (filepath == null)
  154.             return null;
  155.  
  156.         final String[] filepathParts = filepath.split("/");
  157.  
  158.         return filepathParts[filepathParts.length - 1];
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement