|
| 1 | +package ru.nikita.adb; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.List; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Iterator; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.app.ListActivity; |
| 9 | +import android.app.Activity; |
| 10 | +import android.app.AlertDialog; |
| 11 | +import android.content.Context; |
| 12 | +import android.content.DialogInterface; |
| 13 | +import android.widget.BaseAdapter; |
| 14 | +import android.widget.ArrayAdapter; |
| 15 | +import android.widget.TextView; |
| 16 | +import android.widget.EditText; |
| 17 | +import android.widget.ImageView; |
| 18 | +import android.widget.ListView; |
| 19 | +import android.widget.AdapterView; |
| 20 | +import android.widget.Toast; |
| 21 | +import android.view.View; |
| 22 | +import android.view.Menu; |
| 23 | +import android.view.ContextMenu; |
| 24 | +import android.view.MenuItem; |
| 25 | +import android.view.ViewGroup; |
| 26 | +import android.view.LayoutInflater; |
| 27 | +import android.view.MenuInflater; |
| 28 | +import android.view.WindowManager; |
| 29 | +import android.content.Intent; |
| 30 | +import android.text.InputType; |
| 31 | +import ru.nikita.adb.Task; |
| 32 | +import ru.nikita.adb.Binary; |
| 33 | +import ru.nikita.adb.ADBFile; |
| 34 | +import ru.nikita.adb.ADBActivity; |
| 35 | +import ru.nikita.adb.FileManagerActivity; |
| 36 | + |
| 37 | +public class ADBFileManagerActivity extends ListActivity{ |
| 38 | + private static final int FILE_PUSH=1; |
| 39 | + @Override |
| 40 | + public void onCreate(Bundle savedInstanceState){ |
| 41 | + super.onCreate(savedInstanceState); |
| 42 | + Intent intent = getIntent(); |
| 43 | + adb = (Binary) getIntent().getSerializableExtra("adb"); |
| 44 | + device = (Device) getIntent().getSerializableExtra("device"); |
| 45 | + ListView lv = getListView(); |
| 46 | + registerForContextMenu(lv); |
| 47 | + history = new ArrayList<ADBFile>(); |
| 48 | + updateFileList(new ADBFile(adb, device, "/sdcard"), false); |
| 49 | + } |
| 50 | + private void updateFileList(ADBFile file, boolean saveHistory){ |
| 51 | + try{ |
| 52 | + if(saveHistory) |
| 53 | + history.add(currentFile); |
| 54 | + currentFile = file; |
| 55 | + fileList = file.listFiles(); |
| 56 | + setListAdapter(new FileListAdapter(this, fileList)); |
| 57 | + setTitle(file.getPath()); |
| 58 | + }catch(Exception e){ |
| 59 | + makeToast(e.toString()); |
| 60 | + } |
| 61 | + } |
| 62 | + private void makeToast(String text){ |
| 63 | + Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show(); |
| 64 | + } |
| 65 | + @Override |
| 66 | + public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 67 | + if(resultCode == Activity.RESULT_OK && data != null){ |
| 68 | + if(requestCode == FILE_PUSH){ |
| 69 | + String filePath = data.getData().getPath(); |
| 70 | + new ADBTask(adb){ |
| 71 | + @Override |
| 72 | + protected void onPostExecute(String log){ |
| 73 | + updateFileList(currentFile, false); |
| 74 | + } |
| 75 | + }.push(device, filePath, currentFile.getPath()); |
| 76 | + } |
| 77 | + } |
| 78 | + super.onActivityResult(requestCode, resultCode, data); |
| 79 | + } |
| 80 | + @Override |
| 81 | + public boolean onCreateOptionsMenu(Menu menu){ |
| 82 | + getMenuInflater().inflate(R.menu.file_manager_activity,menu); |
| 83 | + return true; |
| 84 | + } |
| 85 | + @Override |
| 86 | + public void onCreateContextMenu(ContextMenu menu, View v, |
| 87 | + ContextMenu.ContextMenuInfo menuInfo) { |
| 88 | + super.onCreateContextMenu(menu, v, menuInfo); |
| 89 | + MenuInflater inflater = getMenuInflater(); |
| 90 | + inflater.inflate(R.menu.file_context_menu, menu); |
| 91 | + } |
| 92 | + @Override |
| 93 | + public boolean onOptionsItemSelected(MenuItem item){ |
| 94 | + int id = item.getItemId(); |
| 95 | + if(id == R.id.refresh_files){ |
| 96 | + updateFileList(currentFile, false); |
| 97 | + return true; |
| 98 | + }else if(id == R.id.file_push){ |
| 99 | + Intent intent = new Intent(this, AppListActivity.class); |
| 100 | + startActivityForResult(intent, FILE_PUSH); |
| 101 | + return true; |
| 102 | + } |
| 103 | + return false; |
| 104 | + } |
| 105 | + @Override |
| 106 | + protected void onListItemClick(ListView l, View v, int position, long id){ |
| 107 | + super.onListItemClick(l,v,position,id); |
| 108 | + ADBFile selected = fileList[position]; |
| 109 | + if(selected.isDirectory()) |
| 110 | + updateFileList(selected, true); |
| 111 | + } |
| 112 | + @Override |
| 113 | + public boolean onContextItemSelected(MenuItem item) { |
| 114 | + final AdapterView.AdapterContextMenuInfo info = |
| 115 | + (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); |
| 116 | + int id = item.getItemId(); |
| 117 | + if(id == R.id.file_delete){ |
| 118 | + AlertDialog.Builder builder = new AlertDialog.Builder(this); |
| 119 | + builder.setTitle(R.string.file_delete_confirm); |
| 120 | + builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 121 | + public void onClick(DialogInterface dialog, int id) { |
| 122 | + try{ |
| 123 | + fileList[info.position].delete(); |
| 124 | + updateFileList(currentFile, false); |
| 125 | + }catch(Exception e){ |
| 126 | + makeToast(e.toString()); |
| 127 | + } |
| 128 | + } |
| 129 | + }); |
| 130 | + builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { |
| 131 | + public void onClick(DialogInterface dialog, int id) { |
| 132 | + dialog.cancel(); |
| 133 | + } |
| 134 | + }); |
| 135 | + builder.show(); |
| 136 | + return true; |
| 137 | + }else if(id == R.id.file_rename){ |
| 138 | + AlertDialog.Builder builder = new AlertDialog.Builder(this); |
| 139 | + builder.setTitle(R.string.file_rename); |
| 140 | + |
| 141 | + final EditText input = new EditText(this); |
| 142 | + input.setText(fileList[info.position].getName()); |
| 143 | + input.setSelectAllOnFocus(true); |
| 144 | + builder.setView(input); |
| 145 | + |
| 146 | + builder.setPositiveButton(android.R.string.ok,new DialogInterface.OnClickListener() { |
| 147 | + @Override |
| 148 | + public void onClick(DialogInterface dialog, int which) { |
| 149 | + String text=input.getText().toString(); |
| 150 | + fileList[info.position].rename(text); |
| 151 | + } |
| 152 | + }); |
| 153 | + builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { |
| 154 | + @Override |
| 155 | + public void onClick(DialogInterface dialog, int which) { |
| 156 | + dialog.cancel(); |
| 157 | + } |
| 158 | + }); |
| 159 | + AlertDialog dialog = builder.create(); |
| 160 | + dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); |
| 161 | + builder.show(); |
| 162 | + return true; |
| 163 | + }else if(id == R.id.file_pull){ |
| 164 | + File dir = new File("/sdcard/ADB"); |
| 165 | + dir.mkdir(); |
| 166 | + fileList[info.position].pull(dir.getPath()); |
| 167 | + return true; |
| 168 | + } |
| 169 | + |
| 170 | + return super.onContextItemSelected(item); |
| 171 | + } |
| 172 | + @Override |
| 173 | + public void onBackPressed(){ |
| 174 | + if(history.isEmpty()) |
| 175 | + finish(); |
| 176 | + else{ |
| 177 | + ADBFile last = history.get(history.size() - 1); |
| 178 | + updateFileList(last, false); |
| 179 | + history.remove(last); |
| 180 | + } |
| 181 | + } |
| 182 | + private class FileListAdapter extends ArrayAdapter<ADBFile>{ |
| 183 | + public FileListAdapter(Context context, ADBFile[] files){ |
| 184 | + super(context,R.layout.file_list_item,files); |
| 185 | + } |
| 186 | + @Override |
| 187 | + public View getView(int i, View view, ViewGroup viewGroup) { |
| 188 | + if(view == null){ |
| 189 | + view = getLayoutInflater().inflate(R.layout.file_list_item, viewGroup, false); |
| 190 | + |
| 191 | + ViewHolder viewHolder = new ViewHolder(); |
| 192 | + viewHolder.iconView = (ImageView) view.findViewById(R.id.file_icon); |
| 193 | + viewHolder.nameView = (TextView) view.findViewById(R.id.file_name); |
| 194 | + |
| 195 | + view.setTag(viewHolder); |
| 196 | + } |
| 197 | + ViewHolder viewHolder = (ViewHolder) view.getTag(); |
| 198 | + ADBFile file = getItem(i); |
| 199 | + |
| 200 | + viewHolder.nameView.setText(file.getName()); |
| 201 | + int icon = file.isDirectory() ? R.drawable.folder : R.drawable.document; |
| 202 | + viewHolder.iconView.setImageResource(icon); |
| 203 | + return view; |
| 204 | + } |
| 205 | + private class ViewHolder{ |
| 206 | + public ImageView iconView; |
| 207 | + public TextView nameView; |
| 208 | + }; |
| 209 | + } |
| 210 | + private ADBFile currentFile; |
| 211 | + private ADBFile[] fileList; |
| 212 | + private List<ADBFile> history; |
| 213 | + private Device device; |
| 214 | + private Binary adb; |
| 215 | +} |
| 216 | + |
| 217 | + |
0 commit comments