[8379cd8] | 1 | /* |
---|
| 2 | Tibisay Movil |
---|
| 3 | |
---|
| 4 | Copyright (C) 2013 Antonio Araujo (aaraujo@cenditel.gob.ve), Jose Ruiz |
---|
| 5 | (jruiz@cenditel.gob.ve), Fundacion Centro Nacional de Desarrollo e |
---|
| 6 | Investigacion en Tecnologias Libres - CENDITEL. |
---|
| 7 | |
---|
| 8 | La Fundación CENDITEL concede permiso para usar, copiar, distribuir y/o |
---|
| 9 | modificar este programa, reconociendo el derecho que la humanidad posee al |
---|
| 10 | libre acceso al conocimiento, bajo los términos de la licencia de software |
---|
| 11 | GPL versión 2.0 de la Free Software Foundation. |
---|
| 12 | |
---|
| 13 | Este programa se distribuye con la esperanza de que sea util, pero SIN |
---|
| 14 | NINGUNA GARANTIA; tampoco las implicitas garantias de MERCANTILIDAD o |
---|
| 15 | ADECUACION A UN PROPOSITO PARTICULAR. |
---|
| 16 | |
---|
| 17 | Para mayor información sobre los términos de la licencia ver el archivo |
---|
| 18 | llamado "gpl-2.0.txt" en ingles. |
---|
| 19 | */ |
---|
| 20 | |
---|
[09c6eb7] | 21 | package ve.gob.cenditel.tibisaymovil; |
---|
| 22 | |
---|
| 23 | import java.io.File; |
---|
| 24 | import java.util.ArrayList; |
---|
| 25 | import java.util.Collections; |
---|
| 26 | import java.util.List; |
---|
| 27 | |
---|
| 28 | import android.app.Activity; |
---|
| 29 | import android.content.Intent; |
---|
| 30 | import android.net.Uri; |
---|
| 31 | import android.os.Bundle; |
---|
| 32 | import android.os.Environment; |
---|
| 33 | import android.view.LayoutInflater; |
---|
| 34 | import android.view.View; |
---|
| 35 | import android.view.ViewGroup; |
---|
| 36 | import android.widget.AdapterView; |
---|
| 37 | import android.widget.AdapterView.OnItemClickListener; |
---|
| 38 | import android.widget.BaseAdapter; |
---|
| 39 | import android.widget.ImageView; |
---|
| 40 | import android.widget.ListView; |
---|
| 41 | import android.widget.TextView; |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * Activity to select a single PKCS12 file, browsing the file system. |
---|
| 45 | * |
---|
| 46 | * @author José M. Prieto (jmprieto@emergya.com) |
---|
| 47 | */ |
---|
| 48 | public class PKCS12FilePickerActivity extends Activity |
---|
| 49 | implements OnItemClickListener { |
---|
| 50 | |
---|
| 51 | private File cwd; |
---|
| 52 | private FileBrowserView viewHolder; |
---|
| 53 | private FileListAdapter listAdapter; |
---|
| 54 | |
---|
| 55 | /** Called when the activity is first created. */ |
---|
| 56 | @Override |
---|
| 57 | public void onCreate(Bundle savedInstanceState) { |
---|
| 58 | |
---|
| 59 | super.onCreate(savedInstanceState); |
---|
| 60 | PKCS12FilePickerActivity.this.viewHolder = new FileBrowserView(); |
---|
| 61 | PKCS12FilePickerActivity.this.viewHolder.fileList.setAdapter( |
---|
| 62 | PKCS12FilePickerActivity.this.listAdapter = new FileListAdapter()); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | @Override |
---|
| 66 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
---|
| 67 | |
---|
| 68 | this.listAdapter.select((ListView) parent, position); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | @Override |
---|
| 72 | public void onBackPressed() { |
---|
| 73 | if ("/".equals(this.cwd.getAbsolutePath())) { |
---|
| 74 | super.onBackPressed(); |
---|
| 75 | } else { |
---|
| 76 | this.viewHolder.fileList.setAdapter(this.listAdapter = new FileListAdapter(cwd.getParent())); |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | @Override |
---|
| 81 | protected void onPause() { |
---|
| 82 | |
---|
| 83 | super.onPause(); |
---|
| 84 | // TODO Auto-generated method stub |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | @Override |
---|
| 88 | protected void onResume() { |
---|
| 89 | |
---|
| 90 | super.onResume(); |
---|
| 91 | // TODO Auto-generated method stub |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /** |
---|
| 95 | * Provides the data to be shown in the file browser ListView. |
---|
| 96 | * |
---|
| 97 | * @author José M. Prieto (jmprieto@emergya.com) |
---|
| 98 | */ |
---|
| 99 | private class FileListAdapter extends BaseAdapter { |
---|
| 100 | |
---|
| 101 | private final List<File> directories; |
---|
| 102 | private final List<File> files; |
---|
| 103 | |
---|
| 104 | private FileListAdapter() { |
---|
| 105 | this(Environment.getExternalStorageDirectory().getAbsolutePath()); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | private FileListAdapter(String location) { |
---|
| 109 | |
---|
| 110 | PKCS12FilePickerActivity.this.viewHolder.path.setText(location); |
---|
| 111 | |
---|
| 112 | directories = new ArrayList<File>(); |
---|
| 113 | files = new ArrayList<File>(); |
---|
| 114 | |
---|
| 115 | PKCS12FilePickerActivity.this.cwd = new File(location); |
---|
| 116 | File parent = PKCS12FilePickerActivity.this.cwd.getParentFile(); |
---|
| 117 | if (parent != null) { |
---|
| 118 | directories.add(0, parent); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | File[] ls = PKCS12FilePickerActivity.this.cwd.listFiles(); |
---|
| 122 | if (ls != null) { |
---|
| 123 | for (File f : ls) { |
---|
| 124 | if (FsUtils.isHiddenOrNotReadable(f)) { |
---|
| 125 | continue; |
---|
| 126 | } |
---|
| 127 | if (f.isDirectory()) { |
---|
| 128 | directories.add(f); |
---|
| 129 | } else { |
---|
| 130 | String name = f.getName(); |
---|
| 131 | if (name.length() >= 4) { |
---|
| 132 | String extension = f.getName().substring(f.getName().length() - 4); |
---|
| 133 | if (extension.equalsIgnoreCase(".p12") || extension.equalsIgnoreCase(".pfx") || true) { |
---|
| 134 | files.add(f); |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | Collections.sort(directories); |
---|
| 142 | Collections.sort(files); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | @Override |
---|
| 146 | public int getCount() { |
---|
| 147 | |
---|
| 148 | return directories.size() + files.size(); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | @Override |
---|
| 152 | public File getItem(int position) { |
---|
| 153 | |
---|
| 154 | if (position < directories.size()) { |
---|
| 155 | return directories.get(position); |
---|
| 156 | } else { |
---|
| 157 | return files.get(position - directories.size()); |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | @Override |
---|
| 162 | public long getItemId(int position) { |
---|
| 163 | |
---|
| 164 | return getItem(position).hashCode(); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | @Override |
---|
| 168 | public View getView(int position, View convertView, ViewGroup parent) { |
---|
| 169 | |
---|
| 170 | if (convertView == null) { |
---|
| 171 | LayoutInflater inflater = LayoutInflater.from(PKCS12FilePickerActivity.this); |
---|
| 172 | //TODO: CANT USE THE SAME LAYOUT AS FILEBROWSER |
---|
| 173 | convertView = inflater.inflate(R.layout.cert_browser_item, parent, false); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | ImageView image = (ImageView) convertView.findViewById(R.id.type_image); |
---|
| 178 | TextView fileName = (TextView) convertView.findViewById(R.id.filename_text); |
---|
| 179 | File file = getItem(position); |
---|
| 180 | |
---|
| 181 | if (file.isDirectory()) { |
---|
[c95e4a6] | 182 | //image.setImageResource(R.drawable.ico_carpeta); |
---|
| 183 | image.setImageResource(R.drawable.ic_carpeta); |
---|
[09c6eb7] | 184 | } else { |
---|
[c95e4a6] | 185 | //image.setImageResource(R.drawable.ico_archivo); |
---|
| 186 | image.setImageResource(R.drawable.ic_archivo); |
---|
[09c6eb7] | 187 | } |
---|
| 188 | |
---|
| 189 | if (file.isDirectory() && position == 0 && ! "/".equals(PKCS12FilePickerActivity.this.cwd.getAbsolutePath())) { |
---|
| 190 | fileName.setText(".."); |
---|
| 191 | } else { |
---|
| 192 | fileName.setText(file.getName()); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | return convertView; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | /** |
---|
| 199 | * Select a file. If the file is a directory, list files in that directory, else hide the file and add it to |
---|
| 200 | * the list of selected files. |
---|
| 201 | * |
---|
| 202 | * @param parent the ListView |
---|
| 203 | * @param position index of the file to remove |
---|
| 204 | */ |
---|
| 205 | public void select(ListView parent, int position) { |
---|
| 206 | |
---|
| 207 | final File item = getItem(position); |
---|
| 208 | if (item.isDirectory()) { |
---|
| 209 | parent.setAdapter(PKCS12FilePickerActivity.this.listAdapter = new FileListAdapter(item.getAbsolutePath())); |
---|
| 210 | } else { |
---|
| 211 | PKCS12FilePickerActivity.this.setResult(Activity.RESULT_OK, new Intent() {{ setData(Uri.fromFile(item)); }}); |
---|
| 212 | PKCS12FilePickerActivity.this.finish(); |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | /** |
---|
| 218 | * Holds references to view objects. |
---|
| 219 | * |
---|
| 220 | * @author José M. Prieto (jmprieto@emergya.com) |
---|
| 221 | */ |
---|
| 222 | private class FileBrowserView { |
---|
| 223 | |
---|
| 224 | public ListView fileList; |
---|
| 225 | public TextView path; |
---|
| 226 | |
---|
| 227 | public FileBrowserView() { |
---|
| 228 | |
---|
| 229 | setContentView(R.layout.single_file_browser); |
---|
| 230 | this.path = (TextView) findViewById(R.id.path); |
---|
| 231 | this.fileList = (ListView) findViewById(R.id.file_list); |
---|
| 232 | this.fileList.setOnItemClickListener(PKCS12FilePickerActivity.this); |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | } |
---|