aboutsummaryrefslogtreecommitdiff
path: root/apps/NotificationStudio/src/com/android/notificationstudio/editor/IconEditor.java
blob: 71e900567e47abb7f6ca4eaea5bd696e261d1f7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.notificationstudio.editor;

import android.content.res.Resources;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import com.android.notificationstudio.R;
import com.android.notificationstudio.editor.Editors.Editor;
import com.android.notificationstudio.model.EditableItem;

public class IconEditor implements Editor {

    public Runnable bindEditor(View v, final EditableItem item, final Runnable afterChange) {
        final LinearLayout iconEditor = (LinearLayout) v.findViewById(R.id.icon_editor_layout);
        final HorizontalScrollView scroller =
                (HorizontalScrollView) v.findViewById(R.id.icon_editor_scroller);
        scroller.setVisibility(View.VISIBLE);

        final Object[] displayValues = getAvailableValuesForDisplay(item);
        final Runnable updateSelection = new Runnable() {
            public void run() {
                for (int i=0;i<iconEditor.getChildCount();i++) {
                    View imageViewHolder = iconEditor.getChildAt(i);
                    Object iconResId = imageViewHolder.getTag();
                    boolean selected = item.hasValue() && item.getValue().equals(iconResId) ||
                            !item.hasValue() && iconResId == null;
                    imageViewHolder.setSelected(selected);
                    if (selected) {
                        int x = imageViewHolder.getLeft();
                        if (x < scroller.getScrollX() ||
                            x > scroller.getScrollX() + scroller.getWidth()) {
                            scroller.scrollTo(imageViewHolder.getLeft(), 0);
                        }
                    }
                }
            }};

        int iconSize = getIconSize(v.getResources());
        int outerMargin = v.getResources().getDimensionPixelSize(R.dimen.editor_icon_outer_margin);
        int innerMargin = v.getResources().getDimensionPixelSize(R.dimen.editor_icon_inner_margin);

        for (final Object iconResId : displayValues) {
            final FrameLayout imageViewHolder = new FrameLayout(v.getContext());
            imageViewHolder.setTag(iconResId);
            final ImageView imageView = new ImageView(v.getContext());
            imageView.setScaleType(ScaleType.CENTER);
            imageView.setOnTouchListener(new OnTouchListener(){
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        v.playSoundEffect(SoundEffectConstants.CLICK);
                        item.setValue(iconResId);
                        updateSelection.run();
                        afterChange.run();
                    }
                    return true;
                }});

            imageViewHolder.setBackgroundResource(R.drawable.icon_bg);
            if (iconResId != null)
                setImage(imageView, iconResId);

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(iconSize, iconSize);
            lp.bottomMargin = lp.topMargin = lp.leftMargin = lp.rightMargin = outerMargin;
            imageViewHolder.setLayoutParams(lp);

            FrameLayout.LayoutParams flp = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            flp.bottomMargin = flp.topMargin = flp.leftMargin = flp.rightMargin = innerMargin;
            imageView.setLayoutParams(flp);

            imageViewHolder.addView(imageView);
            iconEditor.addView(imageViewHolder);
        }
        updateSelection.run();
        return updateSelection;
    }

    protected int getIconSize(Resources res) {
        return res.getDimensionPixelSize(R.dimen.editor_icon_size_small);
    }

    protected void setImage(ImageView imageView, Object value) {
        imageView.setImageResource((Integer) value);
    }

    private Object[] getAvailableValuesForDisplay(EditableItem item) {
        Object[] avail = item.getAvailableValues();
        Object[] rt = new Object[avail.length + 1];
        System.arraycopy(avail, 0, rt, 1, avail.length);
        return rt;
    }

}