Home | History | Annotate | Download | only in activity
      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.content.Intent;
     22 import android.database.DataSetObserver;
     23 import android.os.Bundle;
     24 import android.os.Parcelable;
     25 import android.view.LayoutInflater;
     26 import android.view.View;
     27 import android.view.ViewGroup;
     28 import android.widget.BaseAdapter;
     29 import android.widget.ImageView;
     30 import android.widget.LinearLayout;
     31 import android.widget.ListView;
     32 import android.widget.TextView;
     33 
     34 import com.googlecode.android_scripting.BaseApplication;
     35 import com.googlecode.android_scripting.FeaturedInterpreters;
     36 import com.googlecode.android_scripting.IntentBuilders;
     37 import com.googlecode.android_scripting.R;
     38 import com.googlecode.android_scripting.interpreter.Interpreter;
     39 import com.googlecode.android_scripting.interpreter.InterpreterConfiguration;
     40 import com.googlecode.android_scripting.interpreter.InterpreterConfiguration.ConfigurationObserver;
     41 
     42 import java.util.List;
     43 
     44 /**
     45  * Presents available scripts and returns the selected one.
     46  *
     47  */
     48 public class InterpreterPicker extends ListActivity {
     49 
     50   private List<Interpreter> mInterpreters;
     51   private InterpreterPickerAdapter mAdapter;
     52   private InterpreterConfiguration mConfiguration;
     53   private ScriptListObserver mObserver;
     54 
     55   @Override
     56   public void onCreate(Bundle savedInstanceState) {
     57     super.onCreate(savedInstanceState);
     58     CustomizeWindow.requestCustomTitle(this, "Interpreters", R.layout.script_manager);
     59     mObserver = new ScriptListObserver();
     60     mConfiguration = ((BaseApplication) getApplication()).getInterpreterConfiguration();
     61     mInterpreters = mConfiguration.getInteractiveInterpreters();
     62     mConfiguration.registerObserver(mObserver);
     63     mAdapter = new InterpreterPickerAdapter();
     64     mAdapter.registerDataSetObserver(mObserver);
     65     setListAdapter(mAdapter);
     66   }
     67 
     68   @Override
     69   protected void onListItemClick(ListView list, View view, int position, long id) {
     70     final Interpreter interpreter = (Interpreter) list.getItemAtPosition(position);
     71     if (Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction())) {
     72       int icon =
     73           FeaturedInterpreters.getInterpreterIcon(InterpreterPicker.this, interpreter
     74               .getExtension());
     75       if (icon == 0) {
     76         icon = R.drawable.sl4a_logo_48;
     77       }
     78       Parcelable iconResource =
     79           Intent.ShortcutIconResource.fromContext(InterpreterPicker.this, icon);
     80       Intent intent = IntentBuilders.buildInterpreterShortcutIntent(interpreter, iconResource);
     81       setResult(RESULT_OK, intent);
     82     }
     83     finish();
     84   }
     85 
     86   @Override
     87   protected void onDestroy() {
     88     super.onDestroy();
     89     mConfiguration.unregisterObserver(mObserver);
     90   }
     91 
     92   private class ScriptListObserver extends DataSetObserver implements ConfigurationObserver {
     93     @Override
     94     public void onInvalidated() {
     95       mInterpreters = mConfiguration.getInteractiveInterpreters();
     96     }
     97 
     98     @Override
     99     public void onConfigurationChanged() {
    100       mAdapter.notifyDataSetInvalidated();
    101     }
    102   }
    103 
    104   private class InterpreterPickerAdapter extends BaseAdapter {
    105 
    106     @Override
    107     public int getCount() {
    108       return mInterpreters.size();
    109     }
    110 
    111     @Override
    112     public Object getItem(int position) {
    113       return mInterpreters.get(position);
    114     }
    115 
    116     @Override
    117     public long getItemId(int position) {
    118       return position;
    119     }
    120 
    121     @Override
    122     public View getView(int position, View convertView, ViewGroup parent) {
    123       LinearLayout container;
    124 
    125       Interpreter interpreter = mInterpreters.get(position);
    126 
    127       if (convertView == null) {
    128         LayoutInflater inflater =
    129             (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    130         container = (LinearLayout) inflater.inflate(R.layout.list_item, null);
    131       } else {
    132         container = (LinearLayout) convertView;
    133       }
    134       ImageView img = (ImageView) container.findViewById(R.id.list_item_icon);
    135 
    136       int imgId =
    137           FeaturedInterpreters.getInterpreterIcon(InterpreterPicker.this, interpreter
    138               .getExtension());
    139       if (imgId == 0) {
    140         imgId = R.drawable.sl4a_logo_32;
    141       }
    142 
    143       img.setImageResource(imgId);
    144 
    145       TextView text = (TextView) container.findViewById(R.id.list_item_title);
    146 
    147       text.setText(interpreter.getNiceName());
    148       return container;
    149     }
    150   }
    151 }
    152