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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/*
* Copyright (C) 2012 Christopher Eby <kreed@kreed.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package ch.blinkenlights.android.vanilla;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import com.mobeta.android.dslv.DragSortListView;
/**
* The preferences activity in which one can change application preferences.
*/
public class TabOrderActivity extends Activity
implements View.OnClickListener,
OnItemClickListener,
DragSortListView.DropListener
{
private TabOrderAdapter mAdapter;
private DragSortListView mList;
/**
* Initialize the activity, loading the preference specifications.
*/
@Override
protected void onCreate(Bundle savedInstanceState)
{
ThemeHelper.setTheme(this, R.style.BackActionBar);
super.onCreate(savedInstanceState);
setTitle(R.string.tabs);
setContentView(R.layout.tab_order);
mAdapter = new TabOrderAdapter(this);
DragSortListView list = (DragSortListView)findViewById(R.id.list);
list.setAdapter(mAdapter);
list.setOnItemClickListener(this);
list.setDropListener(this);
mList = list;
load();
findViewById(R.id.done).setOnClickListener(this);
findViewById(R.id.restore_default).setOnClickListener(this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == android.R.id.home) {
finish();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
@Override
public void onClick(View view)
{
switch (view.getId()) {
case R.id.done:
finish();
break;
case R.id.restore_default:
restoreDefault();
break;
}
}
/**
* Restore the default tab order and visibility.
*/
public void restoreDefault()
{
mAdapter.setTabIds(LibraryPagerAdapter.DEFAULT_TAB_ORDER.clone());
DragSortListView list = mList;
for (int i = 0; i != LibraryPagerAdapter.MAX_ADAPTER_COUNT; ++i) {
list.setItemChecked(i, LibraryPagerAdapter.DEFAULT_TAB_VISIBILITY[i]);
}
save();
}
/**
* Save tab order and visibility to SharedPreferences as a string.
*/
public void save()
{
int[] ids = mAdapter.getTabIds();
DragSortListView list = mList;
char[] out = new char[LibraryPagerAdapter.MAX_ADAPTER_COUNT];
for (int i = 0; i != LibraryPagerAdapter.MAX_ADAPTER_COUNT; ++i) {
out[i] = (char)(list.isItemChecked(i) ? 128 + ids[i] : 127 - ids[i]);
}
SharedPreferences.Editor editor = SharedPrefHelper.getSettings(this).edit();
editor.putString(PrefKeys.TAB_ORDER, new String(out));
editor.apply();
}
/**
* Load tab order settings from SharedPreferences and apply it to the
* activity.
*/
public void load()
{
String in = SharedPrefHelper.getSettings(this).getString(PrefKeys.TAB_ORDER, PrefDefaults.TAB_ORDER);
if (in != null && in.length() == LibraryPagerAdapter.MAX_ADAPTER_COUNT) {
char[] chars = in.toCharArray();
int[] ids = new int[LibraryPagerAdapter.MAX_ADAPTER_COUNT];
for (int i = 0; i != LibraryPagerAdapter.MAX_ADAPTER_COUNT; ++i) {
int v = chars[i];
v = v < 128 ? -(v - 127) : v - 128;
if (v >= MediaUtils.TYPE_COUNT) {
ids = null;
break;
}
ids[i] = v;
}
if (ids != null) {
mAdapter.setTabIds(ids);
DragSortListView list = mList;
for (int i = 0; i != LibraryPagerAdapter.MAX_ADAPTER_COUNT; ++i) {
list.setItemChecked(i, chars[i] >= 128);
}
}
return;
}
restoreDefault();
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
save();
}
/**
* Fired from adapter listview if user moved an item
* @param from the item index that was dragged
* @param to the index where the item was dropped
*/
@Override
public void drop(int from, int to) {
if (from == to)
return;
int[] ids = mAdapter.getTabIds();
int tempId = ids[from];
if (from > to) {
System.arraycopy(ids, to, ids, to + 1, from - to);
} else {
System.arraycopy(ids, from + 1, ids, from, to - from);
}
ids[to] = tempId;
save();
mAdapter.notifyDataSetChanged();
// no need to update the copy in mAdapter: We worked on a reference
}
}
|