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 */ 47 public class ScriptPicker extends ListActivity { 48 49 private List<File> mScripts; 50 private ScriptPickerAdapter mAdapter; 51 private InterpreterConfiguration mConfiguration; 52 private File mCurrentDir; 53 private final File mBaseDir = new File(InterpreterConstants.SCRIPTS_ROOT); 54 55 @Override onCreate(Bundle savedInstanceState)56 public void onCreate(Bundle savedInstanceState) { 57 super.onCreate(savedInstanceState); 58 CustomizeWindow.requestCustomTitle(this, "Scripts", R.layout.script_manager); 59 mCurrentDir = mBaseDir; 60 mConfiguration = ((BaseApplication) getApplication()).getInterpreterConfiguration(); 61 mScripts = ScriptStorageAdapter.listExecutableScripts(null, mConfiguration); 62 mAdapter = new ScriptPickerAdapter(this); 63 mAdapter.registerDataSetObserver(new ScriptListObserver()); 64 setListAdapter(mAdapter); 65 } 66 67 @Override onListItemClick(ListView list, View view, int position, long id)68 protected void onListItemClick(ListView list, View view, int position, long id) { 69 final File script = (File) list.getItemAtPosition(position); 70 71 if (script.isDirectory()) { 72 mCurrentDir = script; 73 mAdapter.notifyDataSetInvalidated(); 74 return; 75 } 76 77 //TODO: Take action here based on item click 78 } 79 80 private class ScriptListObserver extends DataSetObserver { 81 @SuppressWarnings("serial") 82 @Override onInvalidated()83 public void onInvalidated() { 84 mScripts = ScriptStorageAdapter.listExecutableScripts(mCurrentDir, mConfiguration); 85 // TODO(damonkohler): Extending the File class here seems odd. 86 if (!mCurrentDir.equals(mBaseDir)) { 87 mScripts.add(0, new File(mCurrentDir.getParent()) { 88 @Override 89 public boolean isDirectory() { 90 return true; 91 } 92 93 @Override 94 public String getName() { 95 return ".."; 96 } 97 }); 98 } 99 } 100 } 101 102 private class ScriptPickerAdapter extends ScriptListAdapter { 103 ScriptPickerAdapter(Context context)104 public ScriptPickerAdapter(Context context) { 105 super(context); 106 } 107 108 @Override getScriptList()109 protected List<File> getScriptList() { 110 return mScripts; 111 } 112 113 } 114 } 115