Skip to content

Most visited

Recently visited

navigation
Notifications / Application / src / com.example.android.support.wearable.notifications /

BackgroundPickers.java

1
/*
2
 * Copyright (C) 2014 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.example.android.support.wearable.notifications;
18
 
19
import android.content.Context;
20
import android.content.res.Resources;
21
import android.view.LayoutInflater;
22
import android.view.View;
23
import android.view.ViewGroup;
24
import android.widget.ImageView;
25
import android.widget.TextView;
26
 
27
import java.util.ArrayList;
28
import java.util.List;
29
 
30
/**
31
 * Manages the background image pickers.
32
 */
33
public class BackgroundPickers {
34
 
35
    public interface OnBackgroundPickersChangedListener {
36
        public void onBackgroundPickersChanged(BackgroundPickers pickers);
37
    }
38
 
39
    private final ViewGroup mContainer;
40
    private final OnPickedListener mOnPickedListener;
41
    private final List<ViewGroup> mPickers;
42
    private final OnBackgroundPickersChangedListener listener;
43
 
44
    public BackgroundPickers(ViewGroup container, OnBackgroundPickersChangedListener listener) {
45
        this.mContainer = container;
46
        this.mOnPickedListener = new OnPickedListener();
47
        this.mPickers = new ArrayList<ViewGroup>();
48
        this.listener = listener;
49
    }
50
 
51
    /**
52
     * Generates the pickers as necessary.
53
     */
54
    public void generatePickers(int count) {
55
        // Clear existing containers.
56
        clear();
57
 
58
        // Fill in new pickers.
59
        LayoutInflater inflater = LayoutInflater.from(mContainer.getContext());
60
        Resources res = mContainer.getResources();
61
        for (int i = 0; i < count; i++) {
62
            View picker = inflater.inflate(R.layout.background_picker, mContainer, false);
63
            TextView label = (TextView) picker.findViewById(R.id.bg_picker_label);
64
            label.setText(String.format(res.getString(R.string.bg_picker_label), i+1));
65
            ViewGroup pickerBox = (ViewGroup) picker.findViewById(R.id.bg_picker_container);
66
            mPickers.add(pickerBox);
67
            for (int j = 0; j < pickerBox.getChildCount(); j++) {
68
                ImageView img = (ImageView) pickerBox.getChildAt(j);
69
                img.setOnClickListener(mOnPickedListener);
70
            }
71
            mContainer.addView(picker);
72
        }
73
    }
74
 
75
    /**
76
     * Returns the background resource for the picker at the given index.
77
     * @param position Index of the background picker.
78
     * @return Id of the background image resource. null if no image is picked.
79
     */
80
    public Integer getRes(int position) {
81
        String tag = (String) mPickers.get(position).getTag();
82
        if (tag == null) {
83
            return null;
84
        }
85
 
86
        Context context = mContainer.getContext();
87
        return context.getResources().getIdentifier(tag, "drawable", context.getPackageName());
88
    }
89
 
90
    /**
91
     * Returns the all the background resources for the pickers managed by this object. Returns null
92
     * if no pickers exist.
93
     */
94
    public Integer[] getRes() {
95
        if (mPickers.size() == 0) {
96
            return null;
97
        }
98
 
99
        Integer[] res = new Integer[mPickers.size()];
100
        for (int i = 0; i < mPickers.size(); i++) {
101
            res[i] = getRes(i);
102
        }
103
        return res;
104
    }
105
 
106
    /**
107
     * Clears the pickers.
108
     */
109
    public void clear() {
110
        mContainer.removeAllViews();
111
        mPickers.clear();
112
    }
113
 
114
    public int getCount() {
115
        return mPickers.size();
116
    }
117
 
118
    private class OnPickedListener implements View.OnClickListener {
119
 
120
        @Override
121
        public void onClick(View view) {
122
            ImageView pickedView = (ImageView) view;
123
            ViewGroup pickerBox = (ViewGroup) view.getParent();
124
 
125
            // Clear old selection.
126
            for (int i = 0; i < pickerBox.getChildCount(); i++) {
127
                ImageView childView = (ImageView) pickerBox.getChildAt(i);
128
                childView.setBackgroundResource(R.drawable.unselected_background);
129
            }
130
 
131
            // Set new selection.
132
            pickedView.setBackgroundResource(R.drawable.selected_background);
133
            pickerBox.setTag(pickedView.getTag());
134
 
135
            if (listener != null) {
136
                listener.onBackgroundPickersChanged(BackgroundPickers.this);
137
            }
138
        }
139
    }
140
}
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.