|
@@ -0,0 +1,420 @@
|
|
|
|
|
+package com.usai.redant.apexdrivers.Upload;
|
|
|
|
|
+
|
|
|
|
|
+import android.content.ComponentName;
|
|
|
|
|
+import android.content.Context;
|
|
|
|
|
+import android.content.DialogInterface;
|
|
|
|
|
+import android.content.Intent;
|
|
|
|
|
+import android.content.ServiceConnection;
|
|
|
|
|
+import android.os.Bundle;
|
|
|
|
|
+import android.os.IBinder;
|
|
|
|
|
+import android.support.annotation.NonNull;
|
|
|
|
|
+import android.support.annotation.Nullable;
|
|
|
|
|
+import android.support.v7.app.ActionBar;
|
|
|
|
|
+import android.support.v7.app.AlertDialog;
|
|
|
|
|
+import android.support.v7.app.AppCompatActivity;
|
|
|
|
|
+import android.util.Log;
|
|
|
|
|
+import android.util.TypedValue;
|
|
|
|
|
+import android.view.Menu;
|
|
|
|
|
+import android.view.MenuItem;
|
|
|
|
|
+import android.view.View;
|
|
|
|
|
+import android.view.ViewGroup;
|
|
|
|
|
+import android.widget.BaseAdapter;
|
|
|
|
|
+import android.widget.ImageButton;
|
|
|
|
|
+import android.widget.ListView;
|
|
|
|
|
+import android.widget.ProgressBar;
|
|
|
|
|
+import android.widget.TextView;
|
|
|
|
|
+
|
|
|
|
|
+import com.usai.redant.apexdrivers.ApexDriversBackgroundService;
|
|
|
|
|
+import com.usai.redant.apexdrivers.R;
|
|
|
|
|
+import com.usai.redant.rautils.Upload.RAUploadManager;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+public class UploadListActivity extends AppCompatActivity {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ private ArrayList<Bundle> displaylist = new ArrayList<Bundle>();
|
|
|
|
|
+
|
|
|
|
|
+ // private SwipeMenuListView uploadList;
|
|
|
|
|
+ private ListView uploadList;
|
|
|
|
|
+ private uploadAdapter adapter;
|
|
|
|
|
+// private ArrayList<Bundle> arr;
|
|
|
|
|
+
|
|
|
|
|
+ private RAUploadManager uploadManager;
|
|
|
|
|
+ private ApexDriversBackgroundService.MyBinder binder;
|
|
|
|
|
+ private ApexDriversBackgroundService uploadServiceservice;
|
|
|
|
|
+ private boolean serviceConnectionFlag = false; // 绑定服务标识
|
|
|
|
|
+ private ServiceConnection serviceConnection;
|
|
|
|
|
+ private RAUploadManager.UIUpdateListener uiUpdateListener = new RAUploadManager.UIUpdateListener() {
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void updateCell(long index, final Bundle taskinfo)
|
|
|
|
|
+ {
|
|
|
|
|
+ if(true)
|
|
|
|
|
+ return;
|
|
|
|
|
+
|
|
|
|
|
+ if(true) {
|
|
|
|
|
+
|
|
|
|
|
+ runOnUiThread(new Runnable() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void run() {
|
|
|
|
|
+
|
|
|
|
|
+ adapter.notifyDataSetChanged();
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ final int position = (int)index;
|
|
|
|
|
+
|
|
|
|
|
+ runOnUiThread(new Runnable() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void run() {
|
|
|
|
|
+
|
|
|
|
|
+ double percent = taskinfo.getDouble("progress",0);
|
|
|
|
|
+
|
|
|
|
|
+ long timeStart = System.currentTimeMillis();
|
|
|
|
|
+
|
|
|
|
|
+ int startShowIndex = uploadList.getFirstVisiblePosition();
|
|
|
|
|
+ int lastShowIndex = uploadList.getLastVisiblePosition();
|
|
|
|
|
+
|
|
|
|
|
+ if (position >= startShowIndex && position <= lastShowIndex) {
|
|
|
|
|
+ View cell = uploadList.getChildAt((int)position - startShowIndex);
|
|
|
|
|
+ uploadAdapter.ViewHolder holder= (uploadAdapter.ViewHolder)cell.getTag();
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ RAUploadManager.TaskStatus istatus = RAUploadManager.TaskStatus.values()[taskinfo.getInt("status",0)];
|
|
|
|
|
+ String status="";
|
|
|
|
|
+ switch(istatus)
|
|
|
|
|
+ {
|
|
|
|
|
+ case TaskStatusStart:
|
|
|
|
|
+ status="uploading";
|
|
|
|
|
+ break;
|
|
|
|
|
+ case TaskStatusStop:
|
|
|
|
|
+ status="stop";
|
|
|
|
|
+ break;
|
|
|
|
|
+ case TaskStatusError:
|
|
|
|
|
+ status="warning";
|
|
|
|
|
+ break;
|
|
|
|
|
+ case TaskStatusWait:
|
|
|
|
|
+ status="waiting";
|
|
|
|
|
+ break;
|
|
|
|
|
+ case TaskStatusFinish:
|
|
|
|
|
+ status="finish";
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ default:
|
|
|
|
|
+ status="warning";
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ String err = taskinfo.getString("msg");
|
|
|
|
|
+ String name = taskinfo.getString("order","");
|
|
|
|
|
+ String action = taskinfo.getString("action","");
|
|
|
|
|
+ String time = taskinfo.getString("time");
|
|
|
|
|
+
|
|
|
|
|
+ holder.name_tv.setText(name);
|
|
|
|
|
+ holder.progressBar.setProgress((int)(100 *percent));
|
|
|
|
|
+ holder.state_tv.setText(status);
|
|
|
|
|
+ holder.action_tv.setText(action);
|
|
|
|
|
+ holder.time_tv.setText(time);
|
|
|
|
|
+ holder.progress_tv.setText(String.format("%.2f%%",100*percent));
|
|
|
|
|
+ holder.err_tv.setText(err);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void updateList(final ArrayList<Bundle> newlist)
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ runOnUiThread(new Runnable() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void run() {
|
|
|
|
|
+
|
|
|
|
|
+ Log.d("_LIST","running tid="+Thread.currentThread().getId());
|
|
|
|
|
+ Log.d("_LIST", "uploadlist UI size: "+ newlist.size());
|
|
|
|
|
+ long timeStart = System.currentTimeMillis();
|
|
|
|
|
+ displaylist = (ArrayList<Bundle>) newlist.clone();
|
|
|
|
|
+ adapter.notifyDataSetChanged();
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
|
|
+ setContentView(R.layout.activity_upload_list);
|
|
|
|
|
+
|
|
|
|
|
+ ActionBar mActionBar = getSupportActionBar();
|
|
|
|
|
+ mActionBar.setHomeButtonEnabled(true);
|
|
|
|
|
+ mActionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
+ mActionBar.setTitle("Apex Drivers");
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // 初始化视图
|
|
|
|
|
+ uploadList = (ListView)findViewById(R.id.upload_list);
|
|
|
|
|
+ adapter = new uploadAdapter(R.layout.upload_cell);
|
|
|
|
|
+ uploadList.setAdapter(adapter);
|
|
|
|
|
+ serviceConnection = new ServiceConnection() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onServiceConnected(ComponentName name, IBinder iBinder) {
|
|
|
|
|
+ serviceConnectionFlag = true;
|
|
|
|
|
+
|
|
|
|
|
+ binder = (ApexDriversBackgroundService.MyBinder)iBinder;
|
|
|
|
|
+ uploadServiceservice = (ApexDriversBackgroundService)binder.getService();
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ uploadManager = uploadServiceservice.uploadManager;
|
|
|
|
|
+ uploadManager.uiUpdateListener = uiUpdateListener;
|
|
|
|
|
+ displaylist = (ArrayList<Bundle>) uploadManager.get_arr_queue().clone(); //uploadManager.arr_queue;
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onServiceDisconnected(ComponentName name) {
|
|
|
|
|
+ serviceConnectionFlag = false;
|
|
|
|
|
+ uploadManager.uiUpdateListener = null;
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ bindService();
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected void onDestroy() {
|
|
|
|
|
+
|
|
|
|
|
+ unbindService(serviceConnection);
|
|
|
|
|
+
|
|
|
|
|
+ super.onDestroy();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
|
+ getMenuInflater().inflate(R.menu.upload_list_menu,menu);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
|
+ switch (item.getItemId()) {
|
|
|
|
|
+ case android.R.id.home: {
|
|
|
|
|
+ finish();
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+ case R.id.clear_upload_list_btn: {
|
|
|
|
|
+
|
|
|
|
|
+ clearUploadList();
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void clearUploadList() {
|
|
|
|
|
+ if (displaylist.size() == 0) {
|
|
|
|
|
+ new AlertDialog.Builder(this)
|
|
|
|
|
+ .setTitle("Warning")
|
|
|
|
|
+ .setMessage("Upload list is empty.")
|
|
|
|
|
+ .setPositiveButton("OK",null)
|
|
|
|
|
+ .show();
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ new AlertDialog.Builder(this)
|
|
|
|
|
+ .setTitle("Clear upload list")
|
|
|
|
|
+ .setMessage("Are you sure remove all error/finish task?")
|
|
|
|
|
+ .setPositiveButton("YES", new DialogInterface.OnClickListener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onClick(DialogInterface dialog, int which) {
|
|
|
|
|
+ // clear
|
|
|
|
|
+ if (serviceConnectionFlag == true && uploadServiceservice != null) {
|
|
|
|
|
+ uploadManager.clearTask();
|
|
|
|
|
+// adapter.notifyDataSetChanged();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .setNegativeButton("NO", new DialogInterface.OnClickListener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onClick(DialogInterface dialog, int which) {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .show();
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ private class uploadAdapter extends BaseAdapter {
|
|
|
|
|
+ private int resourceId;
|
|
|
|
|
+ public uploadAdapter(int resourceID) {
|
|
|
|
|
+
|
|
|
|
|
+ resourceId = resourceID;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int getCount() {
|
|
|
|
|
+ return displaylist.size();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Bundle getItem(int position) {
|
|
|
|
|
+ return displaylist.get(position);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public long getItemId(int position) {
|
|
|
|
|
+ return position;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public int getViewTypeCount() {
|
|
|
|
|
+// return 4;
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public int getItemViewType(int position) {
|
|
|
|
|
+// /**
|
|
|
|
|
+// * 0. <finish> remove </finish>
|
|
|
|
|
+// * 1. <error> start remove </error>
|
|
|
|
|
+// * 2. <stop> start </stop>
|
|
|
|
|
+// * 3. <start\wait> none </start\wait>
|
|
|
|
|
+// * */
|
|
|
|
|
+// return taskSwipeType(position);
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+ @NonNull
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
|
|
|
|
|
+
|
|
|
|
|
+ Log.d("_LIST","running tid="+Thread.currentThread().getId());
|
|
|
|
|
+ long timeStart = System.currentTimeMillis();
|
|
|
|
|
+
|
|
|
|
|
+ final Bundle item = displaylist.get(position);
|
|
|
|
|
+
|
|
|
|
|
+ RAUploadManager.TaskStatus istatus = RAUploadManager.TaskStatus.values()[item.getInt("status",0)];
|
|
|
|
|
+ String status="";
|
|
|
|
|
+ switch(istatus)
|
|
|
|
|
+ {
|
|
|
|
|
+ case TaskStatusStart:
|
|
|
|
|
+ status="uploading";
|
|
|
|
|
+ break;
|
|
|
|
|
+ case TaskStatusStop:
|
|
|
|
|
+ status="stop";
|
|
|
|
|
+ break;
|
|
|
|
|
+ case TaskStatusError:
|
|
|
|
|
+ status="warning";
|
|
|
|
|
+ break;
|
|
|
|
|
+ case TaskStatusWait:
|
|
|
|
|
+ status="waiting";
|
|
|
|
|
+ break;
|
|
|
|
|
+ case TaskStatusFinish:
|
|
|
|
|
+ status="finish";
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ default:
|
|
|
|
|
+ status="warning";
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ double percent = item.getDouble("progress",0);
|
|
|
|
|
+ String err = item.getString("msg");
|
|
|
|
|
+ String name = item.getString("order","");
|
|
|
|
|
+ String action = item.getString("action","");
|
|
|
|
|
+ String time = item.getString("time");
|
|
|
|
|
+
|
|
|
|
|
+ View cell;
|
|
|
|
|
+ ViewHolder holder;
|
|
|
|
|
+ if (convertView != null) {
|
|
|
|
|
+
|
|
|
|
|
+ cell = convertView;
|
|
|
|
|
+ holder = (ViewHolder)cell.getTag();
|
|
|
|
|
+
|
|
|
|
|
+ } else {
|
|
|
|
|
+
|
|
|
|
|
+ cell = View.inflate(getApplicationContext(),resourceId,null);
|
|
|
|
|
+ holder = new ViewHolder();
|
|
|
|
|
+ holder.name_tv = (TextView)cell.findViewById(R.id.upload_cell_title_tv);
|
|
|
|
|
+ holder.action_tv = cell.findViewById(R.id.upload_cell_action_tv);
|
|
|
|
|
+ holder.time_tv = cell.findViewById(R.id.upload_cell_time_tv);
|
|
|
|
|
+ holder.progressBar = (ProgressBar)cell.findViewById(R.id.upload_progressBar);
|
|
|
|
|
+ holder.state_tv = (TextView)cell.findViewById(R.id.upload_state_tv);
|
|
|
|
|
+ holder.progress_tv = (TextView)cell.findViewById(R.id.upload_progress_tv);
|
|
|
|
|
+ holder.err_tv = (TextView)cell.findViewById(R.id.upload_err_tv);
|
|
|
|
|
+ holder.btn_reload=(ImageButton) cell.findViewById(R.id.btn_reload);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ holder.btn_reload.setImageResource(R.drawable.ic_action_reload);
|
|
|
|
|
+ if(istatus!= RAUploadManager.TaskStatus.TaskStatusError)
|
|
|
|
|
+ {
|
|
|
|
|
+ holder.btn_reload.setVisibility(View.GONE);
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ holder.btn_reload.setVisibility(View.VISIBLE);
|
|
|
|
|
+ holder.btn_reload.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onClick(View v) {
|
|
|
|
|
+
|
|
|
|
|
+ uploadManager.startTask(item);
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ cell.setTag(holder);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ holder.name_tv.setText(name);
|
|
|
|
|
+ holder.action_tv.setText(action);
|
|
|
|
|
+ holder.time_tv.setText(time);
|
|
|
|
|
+ holder.progressBar.setProgress((int) (100 * percent));
|
|
|
|
|
+ holder.state_tv.setText(status);
|
|
|
|
|
+ holder.progress_tv.setText(String.format("%.2f%%",100*percent));
|
|
|
|
|
+ holder.err_tv.setText(err);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ Log.d("_LIST", "getView(): POSITION "+position+" COST " + (System.currentTimeMillis() - timeStart));
|
|
|
|
|
+ return cell;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private class ViewHolder {
|
|
|
|
|
+ public TextView name_tv;
|
|
|
|
|
+ public TextView action_tv;
|
|
|
|
|
+ public TextView time_tv;
|
|
|
|
|
+ public ProgressBar progressBar;
|
|
|
|
|
+ public TextView state_tv;
|
|
|
|
|
+ public TextView progress_tv;
|
|
|
|
|
+ public TextView err_tv;
|
|
|
|
|
+ public ImageButton btn_reload;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /** Service */
|
|
|
|
|
+ private void bindService() {
|
|
|
|
|
+ Intent intent = new Intent(UploadListActivity.this, ApexDriversBackgroundService.class);
|
|
|
|
|
+
|
|
|
|
|
+ bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private int dp2px(int dp) {
|
|
|
|
|
+ return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,getResources().getDisplayMetrics());
|
|
|
|
|
+ }
|
|
|
|
|
+}
|