Home | History | Annotate | Download | only in presets
      1 /*
      2  * Copyright (C) 2013 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.android.gallery3d.filtershow.presets;
     18 
     19 import android.content.Context;
     20 import android.graphics.Rect;
     21 import android.view.LayoutInflater;
     22 import android.view.View;
     23 import android.view.ViewGroup;
     24 import android.widget.ArrayAdapter;
     25 import android.widget.EditText;
     26 import android.widget.ImageButton;
     27 import android.widget.ImageView;
     28 import com.android.gallery3d.R;
     29 import com.android.gallery3d.filtershow.category.Action;
     30 import com.android.gallery3d.filtershow.category.CategoryView;
     31 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
     32 import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation;
     33 
     34 import java.util.ArrayList;
     35 
     36 public class UserPresetsAdapter extends ArrayAdapter<Action>
     37         implements View.OnClickListener, View.OnFocusChangeListener {
     38     private static final String LOGTAG = "UserPresetsAdapter";
     39     private LayoutInflater mInflater;
     40     private int mIconSize = 160;
     41     private ArrayList<FilterUserPresetRepresentation> mDeletedRepresentations =
     42             new ArrayList<FilterUserPresetRepresentation>();
     43     private ArrayList<FilterUserPresetRepresentation> mChangedRepresentations =
     44             new ArrayList<FilterUserPresetRepresentation>();
     45     private EditText mCurrentEditText;
     46 
     47     public UserPresetsAdapter(Context context, int textViewResourceId) {
     48         super(context, textViewResourceId);
     49         mInflater = LayoutInflater.from(context);
     50         mIconSize = context.getResources().getDimensionPixelSize(R.dimen.category_panel_icon_size);
     51     }
     52 
     53     public UserPresetsAdapter(Context context) {
     54         this(context, 0);
     55     }
     56 
     57     @Override
     58     public void add(Action action) {
     59         super.add(action);
     60         action.setAdapter(this);
     61     }
     62 
     63     private void deletePreset(Action action) {
     64         FilterRepresentation rep = action.getRepresentation();
     65         if (rep instanceof FilterUserPresetRepresentation) {
     66             mDeletedRepresentations.add((FilterUserPresetRepresentation) rep);
     67         }
     68         remove(action);
     69         notifyDataSetChanged();
     70     }
     71 
     72     private void changePreset(Action action) {
     73         FilterRepresentation rep = action.getRepresentation();
     74         rep.setName(action.getName());
     75         if (rep instanceof FilterUserPresetRepresentation) {
     76             mChangedRepresentations.add((FilterUserPresetRepresentation) rep);
     77         }
     78     }
     79 
     80     public void updateCurrent() {
     81         if (mCurrentEditText != null) {
     82             updateActionFromEditText(mCurrentEditText);
     83         }
     84     }
     85 
     86     static class UserPresetViewHolder {
     87         ImageView imageView;
     88         EditText editText;
     89         ImageButton deleteButton;
     90     }
     91 
     92     @Override
     93     public View getView(int position, View convertView, ViewGroup parent) {
     94         UserPresetViewHolder viewHolder;
     95         if (convertView == null) {
     96             convertView = mInflater.inflate(R.layout.filtershow_presets_management_row, null);
     97             viewHolder = new UserPresetViewHolder();
     98             viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
     99             viewHolder.editText = (EditText) convertView.findViewById(R.id.editView);
    100             viewHolder.deleteButton = (ImageButton) convertView.findViewById(R.id.deleteUserPreset);
    101             viewHolder.editText.setOnClickListener(this);
    102             viewHolder.editText.setOnFocusChangeListener(this);
    103             viewHolder.deleteButton.setOnClickListener(this);
    104             convertView.setTag(viewHolder);
    105         } else {
    106             viewHolder = (UserPresetViewHolder) convertView.getTag();
    107         }
    108         Action action = getItem(position);
    109         viewHolder.imageView.setImageBitmap(action.getImage());
    110         if (action.getImage() == null) {
    111             // queue image rendering for this action
    112             action.setImageFrame(new Rect(0, 0, mIconSize, mIconSize), CategoryView.VERTICAL);
    113         }
    114         viewHolder.deleteButton.setTag(action);
    115         viewHolder.editText.setTag(action);
    116         viewHolder.editText.setHint(action.getName());
    117 
    118         return convertView;
    119     }
    120 
    121     public ArrayList<FilterUserPresetRepresentation> getDeletedRepresentations() {
    122         return mDeletedRepresentations;
    123     }
    124 
    125     public void clearDeletedRepresentations() {
    126         mDeletedRepresentations.clear();
    127     }
    128 
    129     public ArrayList<FilterUserPresetRepresentation> getChangedRepresentations() {
    130         return mChangedRepresentations;
    131     }
    132 
    133     public void clearChangedRepresentations() {
    134         mChangedRepresentations.clear();
    135     }
    136 
    137     @Override
    138     public void onClick(View v) {
    139         switch (v.getId()) {
    140             case R.id.editView:
    141                 v.requestFocus();
    142                 break;
    143             case R.id.deleteUserPreset:
    144                 Action action = (Action) v.getTag();
    145                 deletePreset(action);
    146                 break;
    147         }
    148     }
    149 
    150     @Override
    151     public void onFocusChange(View v, boolean hasFocus) {
    152         if (v.getId() != R.id.editView) {
    153             return;
    154         }
    155         EditText editText = (EditText) v;
    156         if (!hasFocus) {
    157             updateActionFromEditText(editText);
    158         } else {
    159             mCurrentEditText = editText;
    160         }
    161     }
    162 
    163     private void updateActionFromEditText(EditText editText) {
    164         Action action = (Action) editText.getTag();
    165         String newName = editText.getText().toString();
    166         if (newName.length() > 0) {
    167             action.setName(editText.getText().toString());
    168             changePreset(action);
    169         }
    170     }
    171 }
    172