content: package me.lancer.airfree.activity;
  
  import java.io.File;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.List;
  
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.os.Bundle;
  import android.os.Handler;
  import android.os.Message;
  import android.preference.PreferenceManager;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.GridView;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import me.lancer.airfree.adapter.PictureChildAdapter;
  import me.lancer.airfree.util.ApplicationUtil;
  import me.lancer.distance.R;
  
  public class ImageIActivity extends BaseActivity implements View.OnClickListener {
  
      ApplicationUtil app;
      private Button btnBack;
      private TextView tvShow;
      private GridView mGridView;
      private LinearLayout llBottom, btnDelete, btnCopy, btnMove, btnShare, btnAll;
      private TextView tvDelete, tvCopy, tvMove, tvShare, tvAll;
  
      private PictureChildAdapter adapter;
      private List<String> posList = new ArrayList<>();
      private List<String> picList = new ArrayList<>();
      private Boolean isAll = false;
  
      private SharedPreferences pref;
      private String language = "zn";
      private String strConnectionSucceeded = "";
      private String strNoConnection = "";
      private String strConnectionFailed = "";
      private String strShow = "";
      private String strNoInternalExternalStorage = "";
      private String strLoading = "";
      private String strDelete = "";
      private String strCopy = "";
      private String strCut = "";
      private String strUpload = "";
      private String strAll = "";
      private String strPaste = "";
      private String strCancel = "";
  
      public Handler iHandler = new Handler() {
  
          @Override
          public void handleMessage(Message msg) {
              super.handleMessage(msg);
              if (posList.contains(msg.obj)) {
                  posList.remove(msg.obj);
              } else {
                  posList.add((String) msg.obj);
                  Collections.sort(posList, mComparator);
              }
              if (posList.isEmpty()) {
                  llBottom.setVisibility(View.GONE);
              } else {
                  llBottom.setVisibility(View.VISIBLE);
              }
          }
      };
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_image_i);
          init();
      }
  
      public void iLanguage(){
          pref = PreferenceManager.getDefaultSharedPreferences(this);
          language = pref.getString(getString(R.string.language_choice ), "zn");
          if (language.equals("zn")) {
              strConnectionSucceeded = getResources().getString(R.string.connection_succeeded_zn);
              strNoConnection = getResources().getString(R.string.no_connection_zn);
              strConnectionFailed = getResources().getString(R.string.connection_failed_zn);
              strShow = getResources().getString(R.string.image_zn);
              strNoInternalExternalStorage = getResources().getString(R.string.no_internal_external_storage_zn);
              strLoading = getResources().getString(R.string.loading_zn);
              strDelete = getResources().getString(R.string.delete_zn);
              strCopy = getResources().getString(R.string.copy_zn);
              strCut = getResources().getString(R.string.cut_zn);
              strUpload = getResources().getString(R.string.upload_zn);
              strAll = getResources().getString(R.string.all_zn);
              strPaste = getResources().getString(R.string.paste_zn);
              strCancel = getResources().getString(R.string.cancel_zn);
          } else if (language.equals("en")) {
              strConnectionSucceeded = getResources().getString(R.string.connection_succeeded_en);
              strNoConnection = getResources().getString(R.string.no_connection_en);
              strConnectionFailed = getResources().getString(R.string.connection_failed_en);
              strShow = getResources().getString(R.string.image_en);
              strNoInternalExternalStorage = getResources().getString(R.string.no_internal_external_storage_en);
              strLoading = getResources().getString(R.string.loading_en);
              strDelete = getResources().getString(R.string.delete_en);
              strCopy = getResources().getString(R.string.copy_en);
              strCut = getResources().getString(R.string.cut_en);
              strUpload = getResources().getString(R.string.upload_en);
              strAll = getResources().getString(R.string.all_en);
              strPaste = getResources().getString(R.string.paste_en);
              strCancel = getResources().getString(R.string.cancel_en);
          }
      }
  
      private void init() {
          iLanguage();
          app = (ApplicationUtil) ImageIActivity.this.getApplication();
          btnBack = (Button) findViewById(R.id.btn_back);
          btnBack.setOnClickListener(this);
          Intent i = getIntent();
          tvShow = (TextView) findViewById(R.id.tv_show);
          tvShow.setText("" + i.getExtras().get("title"));
          llBottom = (LinearLayout) findViewById(R.id.ll_bottom);
          mGridView = (GridView) findViewById(R.id.gv_child);
          picList = getIntent().getStringArrayListExtra("data");
          Collections.reverse(picList);
          adapter = new PictureChildAdapter(this, picList, posList, mGridView, iHandler);
          mGridView.setAdapter(adapter);
          tvDelete = (TextView) findViewById(R.id.tv_delete);
          tvDelete.setText(strDelete);
          tvCopy = (TextView) findViewById(R.id.tv_copy);
          tvCopy.setText(strCopy);
          tvMove = (TextView) findViewById(R.id.tv_cut);
          tvMove.setText(strCut);
          tvShare = (TextView) findViewById(R.id.tv_upload);
          tvShare.setText(strUpload);
          tvAll = (TextView) findViewById(R.id.tv_all);
          tvAll.setText(strAll);
          btnDelete = (LinearLayout) findViewById(R.id.btn_del);
          btnDelete.setOnClickListener(this);
          btnCopy = (LinearLayout) findViewById(R.id.btn_copy);
          btnCopy.setOnClickListener(this);
          btnMove = (LinearLayout) findViewById(R.id.btn_move);
          btnMove.setOnClickListener(this);
          btnShare = (LinearLayout) findViewById(R.id.btn_share);
          btnShare.setOnClickListener(this);
          btnAll = (LinearLayout) findViewById(R.id.btn_all);
          btnAll.setOnClickListener(this);
      }
  
      @Override
      public void onClick(View v) {
          if (v == btnBack) {
              setResult(RESULT_OK, null);
              finish();
          } else if (v == btnDelete) {
              Handler dHandler = new Handler();
              dHandler.post(deleteFile);
          } else if (v == btnAll) {
              Handler aHandler = new Handler();
              aHandler.post(selectAllFile);
          } else if (v == btnCopy) {
              List<String> portal = new ArrayList<>();
              for (int i = 0; i < posList.size(); i++) {
                  portal.add(picList.get(Integer.parseInt(posList.get(i))));
              }
              Bundle bundle = new Bundle();
              bundle.putString("method", "copy");
              bundle.putStringArrayList("source", (ArrayList<String>) portal);
              Intent intent = new Intent();
              intent.setClass(ImageIActivity.this, MobileActivity.class);
              intent.putExtras(bundle);
              startActivity(intent);
          } else if (v == btnMove) {
              List<String> portal = new ArrayList<>();
              for (int i = 0; i < posList.size(); i++) {
                  portal.add(picList.get(Integer.parseInt(posList.get(i))));
              }
              Bundle bundle = new Bundle();
              bundle.putString("method", "move");
              bundle.putStringArrayList("source", (ArrayList<String>) portal);
              Intent intent = new Intent();
              intent.setClass(ImageIActivity.this, MobileActivity.class);
              intent.putExtras(bundle);
              startActivity(intent);
          } else if (v == btnShare) {
              if (app.getmPrintWriterClient() != null) {
                  pref = PreferenceManager.getDefaultSharedPreferences(this);
                  String ip = pref.getString("ip", "");
                  String port = pref.getString("port", "");
                  String filename = "";
                  for (int i = 0; i < posList.size(); i++) {
                      filename = picList.get(Integer.parseInt(posList.get(i)));
                      sendMessage("file", filename);
                      new SendTask(ip, "59672", filename).execute();
                  }
              } else {
                  Toast.makeText(this, strNoConnection, Toast.LENGTH_SHORT).show();
              }
          }
      }
  
      Runnable deleteFile = new Runnable() {
  
          @Override
          public void run() {
              int count = 0;
              Collections.sort(posList, mComparator);
              while (!posList.isEmpty()) {
                  String deletePath = picList.get(Integer.parseInt(posList.get(0)) - count);
                  File deleteFile = new File(deletePath);
                  Log.e("IP & PORT", "正在删除:" + deletePath);
                  if (deleteFile.exists() && deleteFile.isFile() && deleteFile.canWrite()) {
                      deleteFile.delete();
                      picList.remove(deletePath);
                      posList.remove(posList.get(0));
                      Log.e("IP & PORT", "删除成功!");
                  } else {
                      Log.e("IP & PORT", "删除失败!");
                  }
                  count++;
              }
              adapter.notifyDataSetChanged();
          }
      };
  
      Runnable selectAllFile = new Runnable() {
  
          @Override
          public void run() {
              if (!isAll) {
                  posList.clear();
                  for (int i = 0; i < picList.size(); i++) {
                      posList.add("" + i);
                  }
                  isAll = true;
                  llBottom.setVisibility(View.VISIBLE);
              } else {
                  posList.clear();
                  isAll = false;
                  llBottom.setVisibility(View.GONE);
              }
              adapter.notifyDataSetChanged();
          }
      };
  
      Comparator mComparator = new Comparator() {
          public int compare(Object obj1, Object obj2) {
              String str1 = (String) obj1;
              String str2 = (String) obj2;
              if (Integer.parseInt(str1) < Integer.parseInt(str2))
                  return -1;
              else if (Integer.parseInt(str1) == Integer.parseInt(str2))
                  return 0;
              else if (Integer.parseInt(str1) > Integer.parseInt(str2))
                  return 1;
              return 0;
          }
      };
  }
  