-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactsFragment.java
More file actions
202 lines (165 loc) · 5.58 KB
/
ContactsFragment.java
File metadata and controls
202 lines (165 loc) · 5.58 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
package me.hoen.android_auth_sync;
import java.util.ArrayList;
import me.hoen.android_auth_sync.auth.AccountGeneral;
import me.hoen.android_auth_sync.db.Contact;
import me.hoen.android_auth_sync.db.ContactDataSource;
import me.hoen.android_auth_sync.sync.LoadContactsTask;
import me.hoen.android_auth_sync.sync.SyncAdapter;
import me.hoen.android_auth_sync.sync.SyncTaskCompleted;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class ContactsFragment extends Fragment {
protected ContactAdapter adapter;
protected SwipeRefreshLayout swipeRefreshLayout;
private BroadcastReceiver syncStartReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(MainActivity.TAG, "start sync broadcast received");
swipeRefreshLayout.setRefreshing(true);
}
};
private BroadcastReceiver syncFinishedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(MainActivity.TAG, "end sync broadcast received");
swipeRefreshLayout.setRefreshing(false);
loadData();
}
};
private SyncTaskCompleted syncCallback = new SyncTaskCompleted() {
@Override
public void onSyncCompleted() {
swipeRefreshLayout.setRefreshing(false);
loadData();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_contacts, container,
false);
swipeRefreshLayout = (SwipeRefreshLayout) rootView
.findViewById(R.id.swipe_container);
swipeRefreshLayout.setOnRefreshListener(getSyncListener());
swipeRefreshLayout.setColorSchemeResources(R.color.holo_blue_bright,
R.color.holo_green_light, R.color.holo_orange_light,
R.color.holo_red_light);
ArrayList<Contact> list = new ArrayList<Contact>();
ListView listLv = (ListView) rootView.findViewById(R.id.contacts);
adapter = new ContactAdapter(getActivity(), R.layout.item_contact, list);
listLv.setAdapter(adapter);
return rootView;
}
@Override
public void onResume() {
super.onResume();
getActivity().registerReceiver(syncStartReceiver,
new IntentFilter(SyncAdapter.SYNC_START));
getActivity().registerReceiver(syncFinishedReceiver,
new IntentFilter(SyncAdapter.SYNC_END));
loadData();
}
@Override
public void onPause() {
super.onPause();
getActivity().unregisterReceiver(syncStartReceiver);
getActivity().unregisterReceiver(syncFinishedReceiver);
}
protected SwipeRefreshLayout.OnRefreshListener getSyncListener() {
return new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new LoadContactsTask(getActivity(), syncCallback).execute("");
}
};
}
protected void loadData() {
ContactDataSource eds = new ContactDataSource(getActivity());
ArrayList<Contact> list = eds.getContacts();
eds.close();
adapter.clear();
adapter.addAll(list);
adapter.notifyDataSetChanged();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.contacts, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.profile:
Log.d(MainActivity.TAG, "profile");
Fragment f = new ProfileFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(android.R.id.content, f, "profile").commit();
// seems to prevent fragment to be sometimes displayed one over the
// other.
fragmentManager.executePendingTransactions();
return true;
case R.id.sync:
swipeRefreshLayout.setRefreshing(true);
new LoadContactsTask(getActivity(), syncCallback).execute("");
return true;
case R.id.logout:
new AsyncTask<String, Void, Void>() {
@Override
protected Void doInBackground(String... params) {
try {
// empty contact table, otherwise, there will still be
// previous contacts
ContactDataSource cds = new ContactDataSource(
getActivity());
cds.emptyTable();
cds.close();
AccountManager am = AccountManager.get(getActivity());
Account[] accounts = am
.getAccountsByType(getString(R.string.account_type));
// invalidating token
String authToken = am
.blockingGetAuthToken(
accounts[0],
AccountGeneral.AUTHTOKEN_TYPE_FULL_ACCESS,
true);
am.invalidateAuthToken(
getString(R.string.account_type), authToken);
// deleting account from account manager
am.removeAccount(accounts[0], null, null);
am.clearPassword(accounts[0]);
getActivity().finish();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute("");
return true;
}
return super.onOptionsItemSelected(item);
}
}