[09c6eb7] | 1 | package ve.gob.cenditel.tibisaymovil; |
---|
| 2 | |
---|
| 3 | import java.io.File; |
---|
| 4 | import java.util.ArrayList; |
---|
| 5 | import java.util.Collections; |
---|
| 6 | import java.util.List; |
---|
| 7 | |
---|
| 8 | import android.app.Activity; |
---|
| 9 | import android.content.Intent; |
---|
| 10 | import android.net.Uri; |
---|
| 11 | import android.os.Bundle; |
---|
| 12 | import android.os.Environment; |
---|
| 13 | import android.view.LayoutInflater; |
---|
| 14 | import android.view.View; |
---|
| 15 | import android.view.ViewGroup; |
---|
| 16 | import android.widget.AdapterView; |
---|
| 17 | import android.widget.AdapterView.OnItemClickListener; |
---|
| 18 | import android.widget.BaseAdapter; |
---|
| 19 | import android.widget.ImageView; |
---|
| 20 | import android.widget.ListView; |
---|
| 21 | import android.widget.TextView; |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * Activity to select a single PKCS12 file, browsing the file system. |
---|
| 25 | * |
---|
| 26 | * @author José M. Prieto (jmprieto@emergya.com) |
---|
| 27 | */ |
---|
| 28 | public class PKCS12FilePickerActivity extends Activity |
---|
| 29 | implements OnItemClickListener { |
---|
| 30 | |
---|
| 31 | private File cwd; |
---|
| 32 | private FileBrowserView viewHolder; |
---|
| 33 | private FileListAdapter listAdapter; |
---|
| 34 | |
---|
| 35 | /** Called when the activity is first created. */ |
---|
| 36 | @Override |
---|
| 37 | public void onCreate(Bundle savedInstanceState) { |
---|
| 38 | |
---|
| 39 | super.onCreate(savedInstanceState); |
---|
| 40 | PKCS12FilePickerActivity.this.viewHolder = new FileBrowserView(); |
---|
| 41 | PKCS12FilePickerActivity.this.viewHolder.fileList.setAdapter( |
---|
| 42 | PKCS12FilePickerActivity.this.listAdapter = new FileListAdapter()); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | @Override |
---|
| 46 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
---|
| 47 | |
---|
| 48 | this.listAdapter.select((ListView) parent, position); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | @Override |
---|
| 52 | public void onBackPressed() { |
---|
| 53 | if ("/".equals(this.cwd.getAbsolutePath())) { |
---|
| 54 | super.onBackPressed(); |
---|
| 55 | } else { |
---|
| 56 | this.viewHolder.fileList.setAdapter(this.listAdapter = new FileListAdapter(cwd.getParent())); |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | @Override |
---|
| 61 | protected void onPause() { |
---|
| 62 | |
---|
| 63 | super.onPause(); |
---|
| 64 | // TODO Auto-generated method stub |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | @Override |
---|
| 68 | protected void onResume() { |
---|
| 69 | |
---|
| 70 | super.onResume(); |
---|
| 71 | // TODO Auto-generated method stub |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * Provides the data to be shown in the file browser ListView. |
---|
| 76 | * |
---|
| 77 | * @author José M. Prieto (jmprieto@emergya.com) |
---|
| 78 | */ |
---|
| 79 | private class FileListAdapter extends BaseAdapter { |
---|
| 80 | |
---|
| 81 | private final List<File> directories; |
---|
| 82 | private final List<File> files; |
---|
| 83 | |
---|
| 84 | private FileListAdapter() { |
---|
| 85 | this(Environment.getExternalStorageDirectory().getAbsolutePath()); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | private FileListAdapter(String location) { |
---|
| 89 | |
---|
| 90 | PKCS12FilePickerActivity.this.viewHolder.path.setText(location); |
---|
| 91 | |
---|
| 92 | directories = new ArrayList<File>(); |
---|
| 93 | files = new ArrayList<File>(); |
---|
| 94 | |
---|
| 95 | PKCS12FilePickerActivity.this.cwd = new File(location); |
---|
| 96 | File parent = PKCS12FilePickerActivity.this.cwd.getParentFile(); |
---|
| 97 | if (parent != null) { |
---|
| 98 | directories.add(0, parent); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | File[] ls = PKCS12FilePickerActivity.this.cwd.listFiles(); |
---|
| 102 | if (ls != null) { |
---|
| 103 | for (File f : ls) { |
---|
| 104 | if (FsUtils.isHiddenOrNotReadable(f)) { |
---|
| 105 | continue; |
---|
| 106 | } |
---|
| 107 | if (f.isDirectory()) { |
---|
| 108 | directories.add(f); |
---|
| 109 | } else { |
---|
| 110 | String name = f.getName(); |
---|
| 111 | if (name.length() >= 4) { |
---|
| 112 | String extension = f.getName().substring(f.getName().length() - 4); |
---|
| 113 | if (extension.equalsIgnoreCase(".p12") || extension.equalsIgnoreCase(".pfx") || true) { |
---|
| 114 | files.add(f); |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | Collections.sort(directories); |
---|
| 122 | Collections.sort(files); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | @Override |
---|
| 126 | public int getCount() { |
---|
| 127 | |
---|
| 128 | return directories.size() + files.size(); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | @Override |
---|
| 132 | public File getItem(int position) { |
---|
| 133 | |
---|
| 134 | if (position < directories.size()) { |
---|
| 135 | return directories.get(position); |
---|
| 136 | } else { |
---|
| 137 | return files.get(position - directories.size()); |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | @Override |
---|
| 142 | public long getItemId(int position) { |
---|
| 143 | |
---|
| 144 | return getItem(position).hashCode(); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | @Override |
---|
| 148 | public View getView(int position, View convertView, ViewGroup parent) { |
---|
| 149 | |
---|
| 150 | if (convertView == null) { |
---|
| 151 | LayoutInflater inflater = LayoutInflater.from(PKCS12FilePickerActivity.this); |
---|
| 152 | //TODO: CANT USE THE SAME LAYOUT AS FILEBROWSER |
---|
| 153 | convertView = inflater.inflate(R.layout.cert_browser_item, parent, false); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | ImageView image = (ImageView) convertView.findViewById(R.id.type_image); |
---|
| 158 | TextView fileName = (TextView) convertView.findViewById(R.id.filename_text); |
---|
| 159 | File file = getItem(position); |
---|
| 160 | |
---|
| 161 | if (file.isDirectory()) { |
---|
[c95e4a6] | 162 | //image.setImageResource(R.drawable.ico_carpeta); |
---|
| 163 | image.setImageResource(R.drawable.ic_carpeta); |
---|
[09c6eb7] | 164 | } else { |
---|
[c95e4a6] | 165 | //image.setImageResource(R.drawable.ico_archivo); |
---|
| 166 | image.setImageResource(R.drawable.ic_archivo); |
---|
[09c6eb7] | 167 | } |
---|
| 168 | |
---|
| 169 | if (file.isDirectory() && position == 0 && ! "/".equals(PKCS12FilePickerActivity.this.cwd.getAbsolutePath())) { |
---|
| 170 | fileName.setText(".."); |
---|
| 171 | } else { |
---|
| 172 | fileName.setText(file.getName()); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | return convertView; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | /** |
---|
| 179 | * Select a file. If the file is a directory, list files in that directory, else hide the file and add it to |
---|
| 180 | * the list of selected files. |
---|
| 181 | * |
---|
| 182 | * @param parent the ListView |
---|
| 183 | * @param position index of the file to remove |
---|
| 184 | */ |
---|
| 185 | public void select(ListView parent, int position) { |
---|
| 186 | |
---|
| 187 | final File item = getItem(position); |
---|
| 188 | if (item.isDirectory()) { |
---|
| 189 | parent.setAdapter(PKCS12FilePickerActivity.this.listAdapter = new FileListAdapter(item.getAbsolutePath())); |
---|
| 190 | } else { |
---|
| 191 | PKCS12FilePickerActivity.this.setResult(Activity.RESULT_OK, new Intent() {{ setData(Uri.fromFile(item)); }}); |
---|
| 192 | PKCS12FilePickerActivity.this.finish(); |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | /** |
---|
| 198 | * Holds references to view objects. |
---|
| 199 | * |
---|
| 200 | * @author José M. Prieto (jmprieto@emergya.com) |
---|
| 201 | */ |
---|
| 202 | private class FileBrowserView { |
---|
| 203 | |
---|
| 204 | public ListView fileList; |
---|
| 205 | public TextView path; |
---|
| 206 | |
---|
| 207 | public FileBrowserView() { |
---|
| 208 | |
---|
| 209 | setContentView(R.layout.single_file_browser); |
---|
| 210 | this.path = (TextView) findViewById(R.id.path); |
---|
| 211 | this.fileList = (ListView) findViewById(R.id.file_list); |
---|
| 212 | this.fileList.setOnItemClickListener(PKCS12FilePickerActivity.this); |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | } |
---|