If you think the Android project Locast-Core-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package edu.mit.mobile.android.locast.widget;
/*/*fromwww.java2s.com*/
* Copyright (C) 2010 MIT Mobile Experience Lab
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/import java.util.Date;
import java.util.List;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.widget.ArrayAdapter;
import edu.mit.mobile.android.locast.net.NetworkClient;
publicclass RemoteTagsAdapter extends ArrayAdapter<String>{
privatefinalstaticlong cacheAge = 60 * 1000;
privatelong lastUpdated = 0;
privateboolean updating = false;
final ConnectivityManager cm;
privatefinal NetworkClient mNc;
public RemoteTagsAdapter(Context context, NetworkClient nc, int textViewResourceId) {
super(context, textViewResourceId);
cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
mNc = nc;
}
publicvoid refreshTags(){
final NetworkInfo activeNet = cm.getActiveNetworkInfo();
finalboolean hasNetConnection = activeNet != null && activeNet.isConnected();
if (hasNetConnection && !updating && (lastUpdated == 0 || (new Date().getTime() - lastUpdated > cacheAge))){
new TagLoaderTask().execute();
}
}
privateclass TagLoaderTask extends AsyncTask<Void, Integer, List<String>>{
@Override
protectedvoid onPreExecute() {
updating = true;
}
@Override
protected List<String> doInBackground(Void... params) {
try {
final Context context = getContext();
final List<String> tags = mNc.getTagsList();
lastUpdated = new Date().getTime();
return tags;
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protectedvoid onPostExecute(List<String> result) {
if (result != null){
// bulk load the items into the array
RemoteTagsAdapter.this.setNotifyOnChange(false);
RemoteTagsAdapter.this.clear();
for (final String tag: result){
RemoteTagsAdapter.this.add(tag);
}
RemoteTagsAdapter.this.notifyDataSetChanged();
RemoteTagsAdapter.this.setNotifyOnChange(true);
}
updating = false;
}
}
}