Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / android / source / src / java / org / libreoffice / ui / LibreOfficeUIActivity.java
blob148a98cbafec0a11ee288d1f4b4ddd7229a56bfd
1 /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 package org.libreoffice.ui;
12 import android.app.Activity;
13 import android.content.ComponentName;
14 import android.content.Context;
15 import android.content.Intent;
16 import android.content.SharedPreferences;
17 import android.database.DataSetObserver;
18 import android.net.Uri;
19 import android.os.AsyncTask;
20 import android.os.Bundle;
21 import android.os.Handler;
22 import android.preference.PreferenceManager;
23 import android.support.v4.widget.DrawerLayout;
24 import android.support.v7.app.ActionBar;
25 import android.support.v7.app.ActionBarDrawerToggle;
26 import android.support.v7.app.AppCompatActivity;
27 import android.support.v7.widget.Toolbar;
28 import android.util.Log;
29 import android.view.ContextMenu;
30 import android.view.ContextMenu.ContextMenuInfo;
31 import android.view.LayoutInflater;
32 import android.view.Menu;
33 import android.view.MenuInflater;
34 import android.view.MenuItem;
35 import android.view.View;
36 import android.view.View.OnClickListener;
37 import android.view.View.OnLongClickListener;
38 import android.view.ViewGroup;
39 import android.widget.AdapterView;
40 import android.widget.AdapterView.AdapterContextMenuInfo;
41 import android.widget.AdapterView.OnItemClickListener;
42 import android.widget.ArrayAdapter;
43 import android.widget.GridView;
44 import android.widget.ImageView;
45 import android.widget.LinearLayout;
46 import android.widget.ListAdapter;
47 import android.widget.ListView;
48 import android.widget.TextView;
49 import android.widget.Toast;
51 import org.libreoffice.LOAbout;
52 import org.libreoffice.LibreOfficeMainActivity;
53 import org.libreoffice.R;
54 import org.libreoffice.SettingsActivity;
55 import org.libreoffice.storage.DocumentProviderFactory;
56 import org.libreoffice.storage.DocumentProviderSettingsActivity;
57 import org.libreoffice.storage.IDocumentProvider;
58 import org.libreoffice.storage.IFile;
60 import java.io.File;
61 import java.io.FileFilter;
62 import java.io.FilenameFilter;
63 import java.net.URI;
64 import java.net.URISyntaxException;
65 import java.text.SimpleDateFormat;
66 import java.util.Date;
67 import java.util.List;
69 public class LibreOfficeUIActivity extends AppCompatActivity implements ActionBar.OnNavigationListener {
70 private String LOGTAG = LibreOfficeUIActivity.class.getSimpleName();
71 private SharedPreferences prefs;
72 private int filterMode = FileUtilities.ALL;
73 private int viewMode;
74 private int sortMode;
76 FileFilter fileFilter;
77 FilenameFilter filenameFilter;
78 private List<IFile> filePaths;
79 private DocumentProviderFactory documentProviderFactory;
80 private IDocumentProvider documentProvider;
81 private IFile homeDirectory;
82 private IFile currentDirectory;
84 private static final String CURRENT_DIRECTORY_KEY = "CURRENT_DIRECTORY";
85 private static final String DOC_PROIVDER_KEY = "CURRENT_DOCUMENT_PROVIDER";
86 private static final String FILTER_MODE_KEY = "FILTER_MODE";
87 public static final String EXPLORER_VIEW_TYPE_KEY = "EXPLORER_VIEW_TYPE";
88 public static final String EXPLORER_PREFS_KEY = "EXPLORER_PREFS";
89 public static final String SORT_MODE_KEY = "SORT_MODE";
91 public static final int GRID_VIEW = 0;
92 public static final int LIST_VIEW = 1;
94 private DrawerLayout drawerLayout;
95 private ListView drawerList;
96 private ActionBarDrawerToggle drawerToggle;
97 GridView gv;
98 ListView lv;
100 private final LOAbout mAbout;
101 private boolean canQuit = false;
103 public LibreOfficeUIActivity() {
104 mAbout = new LOAbout(this, true);
107 @Override
108 public void onCreate(Bundle savedInstanceState) {
109 super.onCreate(savedInstanceState);
111 // initialize document provider factory
112 DocumentProviderFactory.initialize(this);
113 documentProviderFactory = DocumentProviderFactory.getInstance();
115 PreferenceManager.setDefaultValues(this, R.xml.documentprovider_preferences, false);
117 readPreferences();
119 // init UI and populate with contents from the provider
120 createUI();
121 switchToDocumentProvider(documentProviderFactory.getDefaultProvider());
124 public void createUI() {
126 setContentView(R.layout.activity_document_browser);
128 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
129 setSupportActionBar(toolbar);
130 ActionBar actionBar = getSupportActionBar();
131 actionBar.setDisplayShowTitleEnabled(false); //This should show current directory if anything
132 actionBar.setDisplayHomeAsUpEnabled(true);
134 //make the navigation spinner
135 Context context = actionBar.getThemedContext();
136 ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.file_view_modes, android.R.layout.simple_spinner_item);
137 list.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
139 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
140 actionBar.setListNavigationCallbacks(list, this);
142 LinearLayout content = (LinearLayout) findViewById(R.id.browser_main_content);
144 if (viewMode == GRID_VIEW) {
145 // code to make a grid view
146 getLayoutInflater().inflate(R.layout.file_grid, content);
147 gv = (GridView)findViewById(R.id.file_explorer_grid_view);
148 gv.setOnItemClickListener(new OnItemClickListener() {
149 public void onItemClick(AdapterView<?> parent, View view,
150 int position, long id) {
151 open(position);
154 actionBar.setSelectedNavigationItem(filterMode + 1);//This triggers the listener which modifies the view.
155 registerForContextMenu(gv);
156 } else {
157 getLayoutInflater().inflate(R.layout.file_list, content);
158 lv = (ListView)findViewById(R.id.file_explorer_list_view);
159 lv.setClickable(true);
160 actionBar.setSelectedNavigationItem(filterMode + 1);
161 registerForContextMenu(lv);
164 // setup the drawer
166 drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
167 drawerList = (ListView) findViewById(R.id.left_drawer);
168 drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
169 R.string.document_locations, R.string.close_document_locations) {
171 @Override
172 public void onDrawerOpened(View drawerView) {
173 super.onDrawerOpened(drawerView);
174 supportInvalidateOptionsMenu();
175 drawerList.requestFocus(); // Make keypad navigation easier
178 @Override
179 public void onDrawerClosed(View drawerView) {
180 super.onDrawerClosed(drawerView);
181 supportInvalidateOptionsMenu();
184 drawerToggle.setDrawerIndicatorEnabled(true);
185 drawerLayout.setDrawerListener(drawerToggle);
186 drawerToggle.syncState();
188 // Set the adapter for the list view
189 drawerList.setAdapter(new ArrayAdapter<String>(this,
190 R.layout.item_in_drawer, documentProviderFactory.getNames()));
191 // Set the list's click listener
192 drawerList.setOnItemClickListener(new ListView.OnItemClickListener() {
193 @Override
194 public void onItemClick(AdapterView parent, View view,
195 int position, long id) {
196 switchToDocumentProvider(documentProviderFactory
197 .getProvider(position));
202 @Override
203 protected void onPostCreate(Bundle savedInstanceState) {
204 super.onPostCreate(savedInstanceState);
206 drawerToggle.syncState();
209 private void refreshView() {
210 // enable home icon as "up" if required
211 if (!currentDirectory.equals(homeDirectory)) {
212 drawerToggle.setDrawerIndicatorEnabled(false);
213 } else {
214 drawerToggle.setDrawerIndicatorEnabled(true);
217 FileUtilities.sortFiles(filePaths, sortMode);
218 // refresh view
219 if (viewMode == GRID_VIEW) {
220 gv.setAdapter(new GridItemAdapter(getApplicationContext(),
221 currentDirectory, filePaths));
222 } else {
223 lv.setAdapter(new ListItemAdapter(getApplicationContext(),
224 filePaths));
226 // close drawer if it was open
227 drawerLayout.closeDrawer(drawerList);
230 @Override
231 public void onBackPressed() {
232 if (drawerLayout.isDrawerOpen(drawerList)) {
233 drawerLayout.closeDrawer(drawerList);
234 } else if (!currentDirectory.equals(homeDirectory)) {
235 // navigate upwards in directory hierarchy
236 openParentDirectory();
237 } else {
238 // only exit if warning has been shown
239 if (canQuit) {
240 super.onBackPressed();
241 return;
244 // show warning about leaving the app and set a timer
245 Toast.makeText(this, R.string.back_again_to_quit,
246 Toast.LENGTH_SHORT).show();
247 canQuit = true;
248 new Handler().postDelayed(new Runnable() {
249 @Override
250 public void run() {
251 canQuit = false;
253 }, 3000);
257 @Override
258 public void onCreateContextMenu(ContextMenu menu, View v,
259 ContextMenuInfo menuInfo) {
260 super.onCreateContextMenu(menu, v, menuInfo);
261 MenuInflater inflater = getMenuInflater();
262 inflater.inflate(R.menu.context_menu, menu);
265 @Override
266 public boolean onContextItemSelected(MenuItem item) {
267 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
268 .getMenuInfo();
269 switch (item.getItemId()) {
270 case R.id.context_menu_open:
271 open(info.position);
272 return true;
273 case R.id.context_menu_share:
274 share(info.position);
275 return true;
276 default:
277 return super.onContextItemSelected(item);
281 private void switchToDocumentProvider(IDocumentProvider provider) {
283 new AsyncTask<IDocumentProvider, Void, Void>() {
284 @Override
285 protected Void doInBackground(IDocumentProvider... provider) {
286 // switch document provider:
287 // these operations may imply network access and must be run in
288 // a different thread
289 try {
290 documentProvider = provider[0];
291 homeDirectory = documentProvider.getRootDirectory();
292 currentDirectory = homeDirectory;
293 filePaths = currentDirectory.listFiles(FileUtilities
294 .getFileFilter(filterMode));
296 catch (final RuntimeException e) {
297 final Activity activity = LibreOfficeUIActivity.this;
298 activity.runOnUiThread(new Runnable() {
299 @Override
300 public void run() {
301 Toast.makeText(activity, e.getMessage(),
302 Toast.LENGTH_SHORT).show();
305 Log.e(LOGTAG, e.getMessage(), e.getCause());
307 return null;
310 @Override
311 protected void onPostExecute(Void result) {
312 refreshView();
314 }.execute(provider);
317 public void openDirectory(IFile dir) {
318 if (dir == null)
319 return;
321 new AsyncTask<IFile, Void, Void>() {
322 @Override
323 protected Void doInBackground(IFile... dir) {
324 // get list of files:
325 // this operation may imply network access and must be run in
326 // a different thread
327 currentDirectory = dir[0];
328 try {
329 filePaths = currentDirectory.listFiles(FileUtilities
330 .getFileFilter(filterMode));
332 catch (final RuntimeException e) {
333 final Activity activity = LibreOfficeUIActivity.this;
334 activity.runOnUiThread(new Runnable() {
335 @Override
336 public void run() {
337 Toast.makeText(activity, e.getMessage(),
338 Toast.LENGTH_SHORT).show();
341 Log.e(LOGTAG, e.getMessage(), e.getCause());
343 return null;
346 @Override
347 protected void onPostExecute(Void result) {
348 refreshView();
350 }.execute(dir);
353 public void open(final IFile document) {
354 new AsyncTask<IFile, Void, File>() {
355 @Override
356 protected File doInBackground(IFile... document) {
357 // this operation may imply network access and must be run in
358 // a different thread
359 try {
360 return document[0].getDocument();
362 catch (final RuntimeException e) {
363 final Activity activity = LibreOfficeUIActivity.this;
364 activity.runOnUiThread(new Runnable() {
365 @Override
366 public void run() {
367 Toast.makeText(activity, e.getMessage(),
368 Toast.LENGTH_SHORT).show();
371 Log.e(LOGTAG, e.getMessage(), e.getCause());
372 return null;
376 @Override
377 protected void onPostExecute(File file) {
378 if (file != null) {
379 Intent i = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
380 String packageName = getApplicationContext().getPackageName();
381 ComponentName componentName = new ComponentName(packageName,
382 LibreOfficeMainActivity.class.getName());
383 i.setComponent(componentName);
385 // these extras allow to rebuild the IFile object in LOMainActivity
386 i.putExtra("org.libreoffice.document_provider_id",
387 documentProvider.getId());
388 i.putExtra("org.libreoffice.document_uri",
389 document.getUri());
391 startActivity(i);
394 }.execute(document);
397 private void open(int position) {
398 IFile file = filePaths.get(position);
399 if (!file.isDirectory()) {
400 open(file);
401 } else {
402 openDirectory(file);
406 private void openParentDirectory() {
407 new AsyncTask<Void, Void, IFile>() {
408 @Override
409 protected IFile doInBackground(Void... dir) {
410 // this operation may imply network access and must be run in
411 // a different thread
412 return currentDirectory.getParent();
415 @Override
416 protected void onPostExecute(IFile result) {
417 openDirectory(result);
419 }.execute();
422 private void share(int position) {
424 new AsyncTask<IFile, Void, File>() {
425 @Override
426 protected File doInBackground(IFile... document) {
427 // this operation may imply network access and must be run in
428 // a different thread
429 try {
430 return document[0].getDocument();
431 } catch (final RuntimeException e) {
432 final Activity activity = LibreOfficeUIActivity.this;
433 activity.runOnUiThread(new Runnable() {
434 @Override
435 public void run() {
436 Toast.makeText(activity, e.getMessage(),
437 Toast.LENGTH_SHORT).show();
440 Log.e(LOGTAG, e.getMessage(), e.getCause());
441 return null;
445 @Override
446 protected void onPostExecute(File file) {
447 if (file != null) {
448 Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
449 Uri uri = Uri.fromFile(file);
450 sharingIntent.setType(FileUtilities.getMimeType(file.getName()));
451 sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM, uri);
452 sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
453 file.getName());
454 startActivity(Intent.createChooser(sharingIntent,
455 getString(R.string.share_via)));
458 }.execute(filePaths.get(position));
461 @Override
462 public boolean onCreateOptionsMenu(Menu menu) {
463 MenuInflater inflater = getMenuInflater();
464 inflater.inflate(R.menu.view_menu, menu);
466 MenuItem item = menu.findItem(R.id.menu_view_toggle);
467 if (viewMode == GRID_VIEW) {
468 item.setTitle(R.string.list_view);
469 item.setIcon(R.drawable.light_view_as_list);
470 } else {
471 item.setTitle(R.string.grid_view);
472 item.setIcon(R.drawable.light_view_as_grid);
475 item = menu.findItem(R.id.menu_sort_size);
476 if (sortMode == FileUtilities.SORT_LARGEST) {
477 item.setTitle(R.string.sort_smallest);
478 } else {
479 item.setTitle(R.string.sort_largest);
482 item = menu.findItem(R.id.menu_sort_az);
483 if (sortMode == FileUtilities.SORT_AZ) {
484 item.setTitle(R.string.sort_za);
485 } else {
486 item.setTitle(R.string.sort_az);
489 item = menu.findItem(R.id.menu_sort_modified);
490 if (sortMode == FileUtilities.SORT_NEWEST) {
491 item.setTitle(R.string.sort_oldest);
492 } else {
493 item.setTitle(R.string.sort_newest);
496 return true;
499 @Override
500 public boolean onOptionsItemSelected(MenuItem item) {
501 // Will close the drawer if the home button is pressed
502 if (drawerToggle.onOptionsItemSelected(item)) {
503 return true;
506 switch (item.getItemId()) {
507 case android.R.id.home:
508 if (!currentDirectory.equals(homeDirectory)){
509 openParentDirectory();
511 break;
512 case R.id.menu_view_toggle:
513 if (viewMode == GRID_VIEW){
514 viewMode = LIST_VIEW;
515 item.setTitle(R.string.grid_view); // Button points to next view.
516 item.setIcon(R.drawable.light_view_as_grid);
517 } else {
518 viewMode = GRID_VIEW;
519 item.setTitle(R.string.list_view); // Button points to next view.
520 item.setIcon(R.drawable.light_view_as_list);
522 createUI();
523 break;
524 case R.id.menu_sort_size:
525 case R.id.menu_sort_az:
526 case R.id.menu_sort_modified:
527 sortFiles(item);
528 break;
529 case R.id.action_about:
530 mAbout.showAbout();
531 return true;
532 case R.id.action_settings:
533 startActivity(new Intent(getApplicationContext(), SettingsActivity.class));
534 return true;
535 case R.id.menu_storage_preferences:
536 startActivity(new Intent(this, DocumentProviderSettingsActivity.class));
537 break;
539 default:
540 return super.onOptionsItemSelected(item);
542 return true;
545 public void sortFiles(MenuItem item){
546 switch (item.getItemId()) {
547 case R.id.menu_sort_az:
548 if (sortMode == FileUtilities.SORT_AZ){
549 sortMode = FileUtilities.SORT_ZA;
550 item.setTitle(R.string.sort_az);
551 } else {
552 sortMode = FileUtilities.SORT_AZ;
553 item.setTitle(R.string.sort_za);
555 break;
556 case R.id.menu_sort_modified:
557 if (sortMode == FileUtilities.SORT_NEWEST){
558 sortMode = FileUtilities.SORT_OLDEST;
559 item.setTitle(R.string.sort_newest);
560 } else {
561 sortMode = FileUtilities.SORT_NEWEST;
562 item.setTitle(R.string.sort_oldest);
564 break;
565 case R.id.menu_sort_size:
566 if (sortMode == FileUtilities.SORT_LARGEST){
567 sortMode = FileUtilities.SORT_SMALLEST;
568 item.setTitle(R.string.sort_largest);
569 } else {
570 sortMode = FileUtilities.SORT_LARGEST;
571 item.setTitle(R.string.sort_smallest);
573 break;
574 default:
575 break;
577 this.onResume();
580 public void readPreferences(){
581 prefs = getSharedPreferences(EXPLORER_PREFS_KEY, MODE_PRIVATE);
582 viewMode = prefs.getInt(EXPLORER_VIEW_TYPE_KEY, GRID_VIEW);
583 sortMode = prefs.getInt(SORT_MODE_KEY, FileUtilities.SORT_AZ);
584 SharedPreferences defaultPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
585 filterMode = Integer.valueOf(defaultPrefs.getString(FILTER_MODE_KEY , "-1"));
587 Intent i = this.getIntent();
588 if (i.hasExtra(CURRENT_DIRECTORY_KEY)) {
589 try {
590 currentDirectory = documentProvider.createFromUri(new URI(
591 i.getStringExtra(CURRENT_DIRECTORY_KEY)));
592 } catch (URISyntaxException e) {
593 currentDirectory = documentProvider.getRootDirectory();
595 Log.d(LOGTAG, CURRENT_DIRECTORY_KEY);
598 if (i.hasExtra(FILTER_MODE_KEY)) {
599 filterMode = i.getIntExtra( FILTER_MODE_KEY, FileUtilities.ALL);
600 Log.d(LOGTAG, FILTER_MODE_KEY);
603 if (i.hasExtra(EXPLORER_VIEW_TYPE_KEY)) {
604 viewMode = i.getIntExtra( EXPLORER_VIEW_TYPE_KEY, GRID_VIEW);
605 Log.d(LOGTAG, EXPLORER_VIEW_TYPE_KEY);
609 @Override
610 protected void onSaveInstanceState(Bundle outState) {
611 // TODO Auto-generated method stub
612 super.onSaveInstanceState(outState);
613 outState.putString(CURRENT_DIRECTORY_KEY, currentDirectory.getUri().toString());
614 outState.putInt(FILTER_MODE_KEY, filterMode);
615 outState.putInt(EXPLORER_VIEW_TYPE_KEY , viewMode);
616 outState.putInt(DOC_PROIVDER_KEY, documentProvider.getId());
618 Log.d(LOGTAG, currentDirectory.toString() + Integer.toString(filterMode) + Integer.toString(viewMode));
619 //prefs.edit().putInt(EXPLORER_VIEW_TYPE, viewType).commit();
620 Log.d(LOGTAG, "savedInstanceState");
623 @Override
624 protected void onRestoreInstanceState(Bundle savedInstanceState) {
625 // TODO Auto-generated method stub
626 super.onRestoreInstanceState(savedInstanceState);
627 if (savedInstanceState.isEmpty()){
628 return;
630 if (documentProvider == null) {
631 Log.d(LOGTAG, "onRestoreInstanceState - documentProvider is null");
632 documentProvider = DocumentProviderFactory.getInstance()
633 .getProvider(savedInstanceState.getInt(DOC_PROIVDER_KEY));
635 try {
636 currentDirectory = documentProvider.createFromUri(new URI(
637 savedInstanceState.getString(CURRENT_DIRECTORY_KEY)));
638 } catch (URISyntaxException e) {
639 currentDirectory = documentProvider.getRootDirectory();
641 filterMode = savedInstanceState.getInt(FILTER_MODE_KEY, FileUtilities.ALL);
642 viewMode = savedInstanceState.getInt(EXPLORER_VIEW_TYPE_KEY, GRID_VIEW);
643 //openDirectory(currentDirectory);
644 Log.d(LOGTAG, "onRestoreInstanceState");
645 Log.d(LOGTAG, currentDirectory.toString() + Integer.toString(filterMode) + Integer.toString(viewMode));
648 @Override
649 protected void onPause() {
650 //prefs.edit().putInt(EXPLORER_VIEW_TYPE, viewType).commit();
651 super.onPause();
652 Log.d(LOGTAG, "onPause");
655 @Override
656 protected void onResume() {
657 super.onResume();
658 Log.d(LOGTAG, "onResume");
659 Log.d(LOGTAG, "sortMode="+ sortMode + " filterMode=" + filterMode);
660 createUI();
663 @Override
664 protected void onStart() {
665 super.onStart();
666 Log.d(LOGTAG, "onStart");
669 @Override
670 protected void onStop() {
671 super.onStop();
672 Log.d(LOGTAG, "onStop");
675 @Override
676 protected void onDestroy() {
677 super.onDestroy();
678 Log.d(LOGTAG, "onDestroy");
681 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
682 filterMode = itemPosition -1; //bit of a hack, I know. -1 is ALL 0 Docs etc
683 openDirectory(currentDirectory);// Uses filter mode
684 return true;
687 private int dpToPx(int dp){
688 final float scale = getApplicationContext().getResources().getDisplayMetrics().density;
689 return (int) (dp * scale + 0.5f);
692 class ListItemAdapter implements ListAdapter{
693 private Context mContext;
694 private List<IFile> filePaths;
695 private final long KB = 1024;
696 private final long MB = 1048576;
698 public ListItemAdapter(Context mContext, List<IFile> filePaths) {
699 this.mContext = mContext;
700 this.filePaths = filePaths;
703 public int getCount() {
704 return filePaths != null ? filePaths.size() : 0;
707 public Object getItem(int arg0) {
708 return null;
711 public long getItemId(int arg0) {
712 return 0;
715 public int getItemViewType(int arg0) {
716 return 0;
719 public View getView(int position, View convertView, ViewGroup parent) {
720 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
721 Context.LAYOUT_INFLATER_SERVICE);
723 View listItem;
725 if (convertView == null) {
726 listItem = new View(mContext);
727 listItem = inflater.inflate(R.layout.file_list_item, null);
728 } else {
729 listItem = convertView;
731 final int pos = position;
732 listItem.setClickable(true);
733 listItem.setOnClickListener(new OnClickListener() {
735 public void onClick(View v) {
736 open(pos);
739 listItem.setOnLongClickListener(new OnLongClickListener() {
741 @Override
742 public boolean onLongClick(View v) {
743 // workaround to show the context menu:
744 // prevent onClickListener from getting this event
745 return false;
752 // set value into textview
753 TextView filename = (TextView) listItem.findViewById(R.id.file_list_item_name);
754 filename.setText(filePaths.get(position).getName());
755 //filename.setClickable(true);
757 TextView fileSize = (TextView) listItem.findViewById(R.id.file_list_item_size);
758 //TODO Give size in KB , MB as appropriate.
759 String size = "0B";
760 long length = filePaths.get(position).getSize();
761 if (length < KB){
762 size = Long.toString(length) + "B";
764 if (length >= KB && length < MB){
765 size = Long.toString(length/KB) + "KB";
767 if (length >= MB){
768 size = Long.toString(length/MB) + "MB";
770 fileSize.setText(size);
771 //fileSize.setClickable(true);
773 TextView fileDate = (TextView) listItem.findViewById(R.id.file_list_item_date);
774 SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy hh:ss");
775 Date date = filePaths.get(position).getLastModified();
776 //TODO format date
777 fileDate.setText(df.format(date));
779 // set image based on selected text
780 ImageView imageView = (ImageView) listItem.findViewById(R.id.file_list_item_icon);
781 switch (FileUtilities.getType(filePaths.get(position).getName()))
783 case FileUtilities.DOC:
784 imageView.setImageResource(R.drawable.writer);
785 break;
786 case FileUtilities.CALC:
787 imageView.setImageResource(R.drawable.calc);
788 break;
789 case FileUtilities.DRAWING:
790 imageView.setImageResource(R.drawable.draw);
791 break;
792 case FileUtilities.IMPRESS:
793 imageView.setImageResource(R.drawable.impress);
794 break;
795 default:
796 break;
798 if (filePaths.get(position).isDirectory()) {
799 //Eventually have thumbnails of each sub file on a black circle
800 //For now just a folder icon
801 imageView.setImageResource(R.drawable.folder);
803 //imageView.setClickable(true);
804 return listItem;
807 public int getViewTypeCount() {
808 return 1;
811 public boolean hasStableIds() {
812 return false;
815 public boolean isEmpty() {
816 return false;
819 public void registerDataSetObserver(DataSetObserver arg0) {
823 public void unregisterDataSetObserver(DataSetObserver arg0) {
827 public boolean areAllItemsEnabled() {
828 return false;
831 public boolean isEnabled(int position) {
832 return false;
838 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */