summaryrefslogtreecommitdiffstats
path: root/tests/RenderScriptTests/ModelViewer/src/com/android/modelviewer/A3DSelector.java
blob: 0e2004f2046ff08c31051827e361f19a7a1e1d72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.modelviewer;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * A list view where the last item the user clicked is placed in
 * the "activated" state, causing its background to highlight.
 */
public class A3DSelector extends ListActivity {

    File[] mCurrentSubList;
    File mCurrentFile;

    class A3DFilter implements FileFilter {
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            }
            return file.getName().endsWith(".a3d");
        }
    }

    private void populateList(File file) {

        mCurrentFile = file;
        setTitle(mCurrentFile.getAbsolutePath() + "/*.a3d");
        List<String> names = new ArrayList<String>();
        names.add("..");

        mCurrentSubList = mCurrentFile.listFiles(new A3DFilter());

        if (mCurrentSubList != null) {
            for (int i = 0; i < mCurrentSubList.length; i ++) {
                String fileName = mCurrentSubList[i].getName();
                if (mCurrentSubList[i].isDirectory()) {
                    fileName = "/" + fileName;
                }
                names.add(fileName);
            }
        }

        // Use the built-in layout for showing a list item with a single
        // line of text whose background is changes when activated.
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_activated_1, names));
        getListView().setTextFilterEnabled(true);

        // Tell the list view to show one checked/activated item at a time.
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        populateList(new File("/sdcard/"));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        if (position == 0) {
            File parent = mCurrentFile.getParentFile();
            if (parent == null) {
                return;
            }
            populateList(parent);
            return;
        }

        // the first thing in list is parent directory
        File selectedFile = mCurrentSubList[position - 1];
        if (selectedFile.isDirectory()) {
            populateList(selectedFile);
            return;
        }

        Intent resultIntent = new Intent();
        resultIntent.setData(Uri.fromFile(selectedFile));
        setResult(RESULT_OK, resultIntent);
        finish();
    }

}