• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.googlecode.android_scripting.activity;
18 
19 import android.app.ListActivity;
20 import android.content.Context;
21 import android.database.DataSetObserver;
22 import android.os.Bundle;
23 import android.view.View;
24 import android.widget.ListView;
25 
26 import com.googlecode.android_scripting.BaseApplication;
27 import com.googlecode.android_scripting.R;
28 import com.googlecode.android_scripting.ScriptListAdapter;
29 import com.googlecode.android_scripting.ScriptStorageAdapter;
30 import com.googlecode.android_scripting.interpreter.InterpreterConfiguration;
31 import com.googlecode.android_scripting.interpreter.InterpreterConstants;
32 
33 import java.io.File;
34 import java.util.List;
35 
36 
37 /**
38  * Presents available scripts and returns the selected one.
39  *
40  */
41 public class ScriptPicker extends ListActivity {
42 
43   private List<File> mScripts;
44   private ScriptPickerAdapter mAdapter;
45   private InterpreterConfiguration mConfiguration;
46   private File mCurrentDir;
47   private final File mBaseDir = new File(InterpreterConstants.SCRIPTS_ROOT);
48 
49   @Override
onCreate(Bundle savedInstanceState)50   public void onCreate(Bundle savedInstanceState) {
51     super.onCreate(savedInstanceState);
52     CustomizeWindow.requestCustomTitle(this, "Scripts", R.layout.script_manager);
53     mCurrentDir = mBaseDir;
54     mConfiguration = ((BaseApplication) getApplication()).getInterpreterConfiguration();
55     mScripts = ScriptStorageAdapter.listExecutableScripts(null, mConfiguration);
56     mAdapter = new ScriptPickerAdapter(this);
57     mAdapter.registerDataSetObserver(new ScriptListObserver());
58     setListAdapter(mAdapter);
59   }
60 
61   @Override
onListItemClick(ListView list, View view, int position, long id)62   protected void onListItemClick(ListView list, View view, int position, long id) {
63     final File script = (File) list.getItemAtPosition(position);
64 
65     if (script.isDirectory()) {
66       mCurrentDir = script;
67       mAdapter.notifyDataSetInvalidated();
68       return;
69     }
70 
71     //TODO: Take action here based on item click
72   }
73 
74   private class ScriptListObserver extends DataSetObserver {
75     @SuppressWarnings("serial")
76     @Override
onInvalidated()77     public void onInvalidated() {
78       mScripts = ScriptStorageAdapter.listExecutableScripts(mCurrentDir, mConfiguration);
79       // TODO(damonkohler): Extending the File class here seems odd.
80       if (!mCurrentDir.equals(mBaseDir)) {
81         mScripts.add(0, new File(mCurrentDir.getParent()) {
82           @Override
83           public boolean isDirectory() {
84             return true;
85           }
86 
87           @Override
88           public String getName() {
89             return "..";
90           }
91         });
92       }
93     }
94   }
95 
96   private class ScriptPickerAdapter extends ScriptListAdapter {
97 
ScriptPickerAdapter(Context context)98     public ScriptPickerAdapter(Context context) {
99       super(context);
100     }
101 
102     @Override
getScriptList()103     protected List<File> getScriptList() {
104       return mScripts;
105     }
106 
107   }
108 }
109