securesmartcam / app / src / main / java / org / witness / obscuracam / ui / adapters / ImageRegionOptionsRecyclerViewAdapter.java @ 41590feb
History | View | Annotate | Download (4.24 KB)
1 |
package org.witness.obscuracam.ui.adapters; |
---|---|
2 |
|
3 |
import android.content.Context; |
4 |
import android.support.v4.widget.TextViewCompat; |
5 |
import android.support.v7.widget.RecyclerView; |
6 |
import android.view.LayoutInflater; |
7 |
import android.view.View; |
8 |
import android.view.ViewGroup; |
9 |
import android.widget.ImageView; |
10 |
import android.widget.TextView; |
11 |
|
12 |
import org.witness.obscuracam.ui.ImageRegion; |
13 |
import org.witness.sscphase1.R; |
14 |
|
15 |
public class ImageRegionOptionsRecyclerViewAdapter extends RecyclerView.Adapter<ImageRegionOptionsRecyclerViewAdapter.OptionViewHolder> { |
16 |
private static final String LOGTAG = "ImageRegionOptionsRVAdapter"; |
17 |
private static final boolean LOGGING = false; |
18 |
|
19 |
public interface ImageRegionOptionsRecyclerViewAdapterListener { |
20 |
void onOptionSelected(int operation); |
21 |
} |
22 |
|
23 |
private final Context mContext; |
24 |
private ImageRegionOptionsRecyclerViewAdapterListener mListener;
|
25 |
private int mCurrentItem = 0; |
26 |
|
27 |
private class ObscureOption { |
28 |
public int name; |
29 |
public int icon; |
30 |
public int operation; |
31 |
public ObscureOption(int name, int icon, int operation) { |
32 |
this.name = name;
|
33 |
this.icon = icon;
|
34 |
this.operation = operation;
|
35 |
} |
36 |
} |
37 |
|
38 |
private ObscureOption[] options = new ObscureOption[] { |
39 |
new ObscureOption(R.string.obscure_option_redact, R.drawable.ic_context_fill, ImageRegion.REDACT),
|
40 |
new ObscureOption(R.string.obscure_option_pixelate, R.drawable.ic_context_pixelate, ImageRegion.PIXELATE),
|
41 |
new ObscureOption(R.string.obscure_option_inverse, R.drawable.ic_context_pixelate_crowd, ImageRegion.BG_PIXELATE),
|
42 |
new ObscureOption(R.string.obscure_option_mask, R.drawable.ic_context_mask, ImageRegion.MASK),
|
43 |
new ObscureOption(R.string.obscure_option_remove, R.drawable.ic_context_delete, -1) |
44 |
}; |
45 |
|
46 |
public ImageRegionOptionsRecyclerViewAdapter(Context context) { |
47 |
super();
|
48 |
mContext = context; |
49 |
setHasStableIds(true);
|
50 |
} |
51 |
|
52 |
protected Context getContext() { |
53 |
return mContext;
|
54 |
} |
55 |
|
56 |
public void setListener(ImageRegionOptionsRecyclerViewAdapterListener listener) { |
57 |
mListener = listener; |
58 |
} |
59 |
|
60 |
public void setCurrentItem(int operation) { |
61 |
mCurrentItem = operation; |
62 |
notifyDataSetChanged(); |
63 |
} |
64 |
|
65 |
@Override
|
66 |
public int getItemCount() { |
67 |
return options.length;
|
68 |
} |
69 |
|
70 |
@Override
|
71 |
public long getItemId(int position) { |
72 |
return options[position].name;
|
73 |
} |
74 |
|
75 |
@Override
|
76 |
public int getItemViewType(int position) { |
77 |
return 0; |
78 |
} |
79 |
|
80 |
@Override
|
81 |
public OptionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
82 |
View view = LayoutInflater.from(mContext).inflate(R.layout.region_option_item, parent, false); |
83 |
return new OptionViewHolder(view); |
84 |
} |
85 |
|
86 |
@Override
|
87 |
public void onBindViewHolder(OptionViewHolder holder, int position) { |
88 |
ObscureOption option = options[position]; |
89 |
holder.name.setText(option.name); |
90 |
holder.itemView.setOnClickListener(new ItemClickListener(option));
|
91 |
if (option.operation == mCurrentItem) {
|
92 |
TextViewCompat.setTextAppearance(holder.name, R.style.TextAppearanceObscureOptionSelected); |
93 |
holder.icon.setBackgroundResource(R.drawable.region_option_bg_selected); |
94 |
} else {
|
95 |
TextViewCompat.setTextAppearance(holder.name, R.style.TextAppearanceObscureOption); |
96 |
holder.icon.setBackgroundResource(R.drawable.region_option_bg); |
97 |
} |
98 |
holder.icon.setImageResource(option.icon); |
99 |
} |
100 |
|
101 |
private class ItemClickListener implements View.OnClickListener { |
102 |
private final ObscureOption option; |
103 |
|
104 |
public ItemClickListener(ObscureOption option) {
|
105 |
this.option = option;
|
106 |
} |
107 |
|
108 |
@Override
|
109 |
public void onClick(View view) { |
110 |
if (mListener != null) { |
111 |
mListener.onOptionSelected(this.option.operation);
|
112 |
} |
113 |
} |
114 |
} |
115 |
|
116 |
class OptionViewHolder extends RecyclerView.ViewHolder { |
117 |
public final ImageView icon; |
118 |
public final TextView name; |
119 |
public OptionViewHolder(View itemView) { |
120 |
super(itemView);
|
121 |
icon = (ImageView) itemView.findViewById(R.id.icon);
|
122 |
name = (TextView) itemView.findViewById(R.id.name); |
123 |
} |
124 |
} |
125 |
} |