• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * 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.content.Intent;
22 import android.database.DataSetObserver;
23 import android.os.Bundle;
24 import android.os.Parcelable;
25 import android.view.View;
26 import android.view.View.OnClickListener;
27 import android.widget.ListView;
28 
29 import com.googlecode.android_scripting.BaseApplication;
30 import com.googlecode.android_scripting.Constants;
31 import com.googlecode.android_scripting.FeaturedInterpreters;
32 import com.googlecode.android_scripting.IntentBuilders;
33 import com.googlecode.android_scripting.R;
34 import com.googlecode.android_scripting.ScriptListAdapter;
35 import com.googlecode.android_scripting.ScriptStorageAdapter;
36 import com.googlecode.android_scripting.interpreter.InterpreterConfiguration;
37 import com.googlecode.android_scripting.interpreter.InterpreterConstants;
38 
39 import java.io.File;
40 import java.util.List;
41 
42 
43 /**
44  * Presents available scripts and returns the selected one.
45  *
46  * @author Damon Kohler (damonkohler@gmail.com)
47  */
48 public class ScriptPicker extends ListActivity {
49 
50   private List<File> mScripts;
51   private ScriptPickerAdapter mAdapter;
52   private InterpreterConfiguration mConfiguration;
53   private File mCurrentDir;
54   private final File mBaseDir = new File(InterpreterConstants.SCRIPTS_ROOT);
55 
56   @Override
onCreate(Bundle savedInstanceState)57   public void onCreate(Bundle savedInstanceState) {
58     super.onCreate(savedInstanceState);
59     CustomizeWindow.requestCustomTitle(this, "Scripts", R.layout.script_manager);
60     mCurrentDir = mBaseDir;
61     mConfiguration = ((BaseApplication) getApplication()).getInterpreterConfiguration();
62     mScripts = ScriptStorageAdapter.listExecutableScripts(null, mConfiguration);
63     mAdapter = new ScriptPickerAdapter(this);
64     mAdapter.registerDataSetObserver(new ScriptListObserver());
65     setListAdapter(mAdapter);
66   }
67 
68   @Override
onListItemClick(ListView list, View view, int position, long id)69   protected void onListItemClick(ListView list, View view, int position, long id) {
70     final File script = (File) list.getItemAtPosition(position);
71 
72     if (script.isDirectory()) {
73       mCurrentDir = script;
74       mAdapter.notifyDataSetInvalidated();
75       return;
76     }
77 
78     //TODO: Take action here based on item click
79   }
80 
81   private class ScriptListObserver extends DataSetObserver {
82     @SuppressWarnings("serial")
83     @Override
onInvalidated()84     public void onInvalidated() {
85       mScripts = ScriptStorageAdapter.listExecutableScripts(mCurrentDir, mConfiguration);
86       // TODO(damonkohler): Extending the File class here seems odd.
87       if (!mCurrentDir.equals(mBaseDir)) {
88         mScripts.add(0, new File(mCurrentDir.getParent()) {
89           @Override
90           public boolean isDirectory() {
91             return true;
92           }
93 
94           @Override
95           public String getName() {
96             return "..";
97           }
98         });
99       }
100     }
101   }
102 
103   private class ScriptPickerAdapter extends ScriptListAdapter {
104 
ScriptPickerAdapter(Context context)105     public ScriptPickerAdapter(Context context) {
106       super(context);
107     }
108 
109     @Override
getScriptList()110     protected List<File> getScriptList() {
111       return mScripts;
112     }
113 
114   }
115 }
116