IconEditor.java revision 3e190ede751474c7e1b87b34e36354c0ef9e8af4
1/*
2 * Copyright 2012 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
17package com.android.notificationstudio.editor;
18
19import android.content.res.Resources;
20import android.view.MotionEvent;
21import android.view.SoundEffectConstants;
22import android.view.View;
23import android.view.View.OnTouchListener;
24import android.view.ViewGroup;
25import android.widget.FrameLayout;
26import android.widget.HorizontalScrollView;
27import android.widget.ImageView;
28import android.widget.ImageView.ScaleType;
29import android.widget.LinearLayout;
30
31import com.android.notificationstudio.R;
32import com.android.notificationstudio.editor.Editors.Editor;
33import com.android.notificationstudio.model.EditableItem;
34
35public class IconEditor implements Editor {
36
37    public Runnable bindEditor(View v, final EditableItem item, final Runnable afterChange) {
38        final LinearLayout iconEditor = (LinearLayout) v.findViewById(R.id.icon_editor_layout);
39        final HorizontalScrollView scroller =
40                (HorizontalScrollView) v.findViewById(R.id.icon_editor_scroller);
41        scroller.setVisibility(View.VISIBLE);
42
43        final Object[] displayValues = getAvailableValuesForDisplay(item);
44        final Runnable updateSelection = new Runnable() {
45            public void run() {
46                for (int i=0;i<iconEditor.getChildCount();i++) {
47                    View imageViewHolder = iconEditor.getChildAt(i);
48                    Object iconResId = imageViewHolder.getTag();
49                    boolean selected = item.hasValue() && item.getValue().equals(iconResId) ||
50                            !item.hasValue() && iconResId == null;
51                    imageViewHolder.setSelected(selected);
52                    if (selected) {
53                        int x = imageViewHolder.getLeft();
54                        if (x < scroller.getScrollX() ||
55                            x > scroller.getScrollX() + scroller.getWidth()) {
56                            scroller.scrollTo(imageViewHolder.getLeft(), 0);
57                        }
58                    }
59                }
60            }};
61
62        int iconSize = getIconSize(v.getResources());
63        int outerMargin = v.getResources().getDimensionPixelSize(R.dimen.editor_icon_outer_margin);
64        int innerMargin = v.getResources().getDimensionPixelSize(R.dimen.editor_icon_inner_margin);
65
66        for (final Object iconResId : displayValues) {
67            final FrameLayout imageViewHolder = new FrameLayout(v.getContext());
68            imageViewHolder.setTag(iconResId);
69            final ImageView imageView = new ImageView(v.getContext());
70            imageView.setScaleType(ScaleType.CENTER);
71            imageView.setOnTouchListener(new OnTouchListener(){
72                public boolean onTouch(View v, MotionEvent event) {
73                    if (event.getAction() == MotionEvent.ACTION_UP) {
74                        v.playSoundEffect(SoundEffectConstants.CLICK);
75                        item.setValue(iconResId);
76                        updateSelection.run();
77                        afterChange.run();
78                    }
79                    return true;
80                }});
81
82            imageViewHolder.setBackgroundResource(R.drawable.icon_bg);
83            if (iconResId != null)
84                setImage(imageView, iconResId);
85
86            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(iconSize, iconSize);
87            lp.bottomMargin = lp.topMargin = lp.leftMargin = lp.rightMargin = outerMargin;
88            imageViewHolder.setLayoutParams(lp);
89
90            FrameLayout.LayoutParams flp = new FrameLayout.LayoutParams(
91                    ViewGroup.LayoutParams.MATCH_PARENT,
92                    ViewGroup.LayoutParams.MATCH_PARENT);
93            flp.bottomMargin = flp.topMargin = flp.leftMargin = flp.rightMargin = innerMargin;
94            imageView.setLayoutParams(flp);
95
96            imageViewHolder.addView(imageView);
97            iconEditor.addView(imageViewHolder);
98        }
99        updateSelection.run();
100        return updateSelection;
101    }
102
103    protected int getIconSize(Resources res) {
104        return res.getDimensionPixelSize(R.dimen.editor_icon_size_small);
105    }
106
107    protected void setImage(ImageView imageView, Object value) {
108        imageView.setImageResource((Integer) value);
109    }
110
111    private Object[] getAvailableValuesForDisplay(EditableItem item) {
112        Object[] avail = item.getAvailableValues();
113        Object[] rt = new Object[avail.length + 1];
114        System.arraycopy(avail, 0, rt, 1, avail.length);
115        return rt;
116    }
117
118}