Home | History | Annotate | Download | only in testapp
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.testapp;
     18 
     19 import java.io.File;
     20 import java.io.FileFilter;
     21 import java.util.ArrayList;
     22 import java.util.List;
     23 
     24 import android.app.ListActivity;
     25 import android.content.Intent;
     26 import android.net.Uri;
     27 import android.os.Bundle;
     28 import android.view.View;
     29 import android.widget.ArrayAdapter;
     30 import android.widget.ListView;
     31 
     32 /**
     33  * A list view where the last item the user clicked is placed in
     34  * the "activated" state, causing its background to highlight.
     35  */
     36 public class FileSelector extends ListActivity {
     37 
     38     File[] mCurrentSubList;
     39     File mCurrentFile;
     40 
     41     class DAEFilter implements FileFilter {
     42         public boolean accept(File file) {
     43             if (file.isDirectory()) {
     44                 return true;
     45             }
     46             return file.getName().endsWith(".dae");
     47         }
     48     }
     49 
     50     private void populateList(File file) {
     51 
     52         mCurrentFile = file;
     53         setTitle(mCurrentFile.getAbsolutePath() + "/*.dae");
     54         List<String> names = new ArrayList<String>();
     55         names.add("..");
     56 
     57         mCurrentSubList = mCurrentFile.listFiles(new DAEFilter());
     58 
     59         if (mCurrentSubList != null) {
     60             for (int i = 0; i < mCurrentSubList.length; i ++) {
     61                 String fileName = mCurrentSubList[i].getName();
     62                 if (mCurrentSubList[i].isDirectory()) {
     63                     fileName = "/" + fileName;
     64                 }
     65                 names.add(fileName);
     66             }
     67         }
     68 
     69         // Use the built-in layout for showing a list item with a single
     70         // line of text whose background is changes when activated.
     71         setListAdapter(new ArrayAdapter<String>(this,
     72                 android.R.layout.simple_list_item_activated_1, names));
     73         getListView().setTextFilterEnabled(true);
     74 
     75         // Tell the list view to show one checked/activated item at a time.
     76         getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
     77     }
     78 
     79     @Override
     80     public void onCreate(Bundle savedInstanceState) {
     81         super.onCreate(savedInstanceState);
     82 
     83         populateList(new File("/sdcard/"));
     84     }
     85 
     86     @Override
     87     protected void onListItemClick(ListView l, View v, int position, long id) {
     88         if (position == 0) {
     89             File parent = mCurrentFile.getParentFile();
     90             if (parent == null) {
     91                 return;
     92             }
     93             populateList(parent);
     94             return;
     95         }
     96 
     97         // the first thing in list is parent directory
     98         File selectedFile = mCurrentSubList[position - 1];
     99         if (selectedFile.isDirectory()) {
    100             populateList(selectedFile);
    101             return;
    102         }
    103 
    104         Intent resultIntent = new Intent();
    105         resultIntent.setData(Uri.fromFile(selectedFile));
    106         setResult(RESULT_OK, resultIntent);
    107         finish();
    108     }
    109 
    110 }
    111