Show number of available updates in tab heading

This commit is contained in:
Ciaran Gultnieks 2010-12-16 22:44:26 +00:00
parent bec725da56
commit a5d4399e28

View File

@ -60,415 +60,423 @@ import android.widget.TabHost.TabSpec;
public class FDroid extends TabActivity implements OnItemClickListener { public class FDroid extends TabActivity implements OnItemClickListener {
private class AppListAdapter extends BaseAdapter { private class AppListAdapter extends BaseAdapter {
private List<DB.App> items = new ArrayList<DB.App>(); private List<DB.App> items = new ArrayList<DB.App>();
public AppListAdapter(Context context) { public AppListAdapter(Context context) {
} }
public void addItem(DB.App app) { public void addItem(DB.App app) {
items.add(app); items.add(app);
} }
public void clear() { public void clear() {
items.clear(); items.clear();
} }
@Override @Override
public int getCount() { public int getCount() {
return items.size(); return items.size();
} }
@Override @Override
public Object getItem(int position) { public Object getItem(int position) {
return items.get(position); return items.get(position);
} }
@Override @Override
public long getItemId(int position) { public long getItemId(int position) {
return position; return position;
} }
@Override @Override
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView; View v = convertView;
if (v == null) { if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.applistitem, null); v = vi.inflate(R.layout.applistitem, null);
} }
DB.App app = items.get(position); DB.App app = items.get(position);
TextView name = (TextView) v.findViewById(R.id.name); TextView name = (TextView) v.findViewById(R.id.name);
name.setText(app.name); name.setText(app.name);
String vs; String vs;
int numav = app.apks.size(); int numav = app.apks.size();
if (numav == 1) if (numav == 1)
vs = getString(R.string.n_version_available); vs = getString(R.string.n_version_available);
else else
vs = getString(R.string.n_versions_available); vs = getString(R.string.n_versions_available);
TextView status = (TextView) v.findViewById(R.id.status); TextView status = (TextView) v.findViewById(R.id.status);
status.setText(String.format(vs, numav)); status.setText(String.format(vs, numav));
TextView license = (TextView) v.findViewById(R.id.license); TextView license = (TextView) v.findViewById(R.id.license);
license.setText(app.license); license.setText(app.license);
TextView summary = (TextView) v.findViewById(R.id.summary); TextView summary = (TextView) v.findViewById(R.id.summary);
summary.setText(app.summary); summary.setText(app.summary);
ImageView icon = (ImageView) v.findViewById(R.id.icon); ImageView icon = (ImageView) v.findViewById(R.id.icon);
String iconpath = new String(DB.getIconsPath() + app.icon); String iconpath = new String(DB.getIconsPath() + app.icon);
File icn = new File(iconpath); File icn = new File(iconpath);
if (icn.exists() && icn.length() > 0) { if (icn.exists() && icn.length() > 0) {
new Uri.Builder().build(); new Uri.Builder().build();
icon.setImageURI(Uri.parse(iconpath)); icon.setImageURI(Uri.parse(iconpath));
} else { } else {
icon.setImageResource(android.R.drawable.sym_def_app_icon); icon.setImageResource(android.R.drawable.sym_def_app_icon);
} }
return v; return v;
} }
} }
private String LOCAL_PATH = "/sdcard/.fdroid"; private String LOCAL_PATH = "/sdcard/.fdroid";
private static final int REQUEST_APPDETAILS = 0; private static final int REQUEST_APPDETAILS = 0;
private static final int REQUEST_MANAGEREPOS = 1; private static final int REQUEST_MANAGEREPOS = 1;
private static final int REQUEST_PREFS = 2; private static final int REQUEST_PREFS = 2;
private static final int UPDATE_REPO = Menu.FIRST; private static final int UPDATE_REPO = Menu.FIRST;
private static final int MANAGE_REPO = Menu.FIRST + 1; private static final int MANAGE_REPO = Menu.FIRST + 1;
private static final int PREFERENCES = Menu.FIRST + 2; private static final int PREFERENCES = Menu.FIRST + 2;
private static final int ABOUT = Menu.FIRST + 3; private static final int ABOUT = Menu.FIRST + 3;
private DB db = null; private DB db = null;
// Apps that are available to be installed // Apps that are available to be installed
private AppListAdapter apps_av = new AppListAdapter(this); private AppListAdapter apps_av = new AppListAdapter(this);
// Apps that are installed // Apps that are installed
private AppListAdapter apps_in = new AppListAdapter(this); private AppListAdapter apps_in = new AppListAdapter(this);
// Apps that can be upgraded // Apps that can be upgraded
private AppListAdapter apps_up = new AppListAdapter(this); private AppListAdapter apps_up = new AppListAdapter(this);
private ProgressDialog pd; private ProgressDialog pd;
private static final String TAB_IN = "INST"; private static final String TAB_IN = "INST";
private static final String TAB_UN = "UNIN"; private static final String TAB_UN = "UNIN";
private static final String TAB_UP = "UPDT"; private static final String TAB_UP = "UPDT";
private TabHost tabHost; private TabHost tabHost;
private TabSpec ts; private TabSpec ts;
private TabSpec ts1; private TabSpec ts1;
private TabSpec tsUp; private TabSpec tsUp;
private boolean triedEmptyUpdate; private boolean triedEmptyUpdate;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.fdroid); setContentView(R.layout.fdroid);
File local_path = new File(LOCAL_PATH); File local_path = new File(LOCAL_PATH);
if (!local_path.exists()) if (!local_path.exists())
local_path.mkdir(); local_path.mkdir();
File icon_path = new File(DB.getIconsPath()); File icon_path = new File(DB.getIconsPath());
if (!icon_path.exists()) if (!icon_path.exists())
icon_path.mkdir(); icon_path.mkdir();
tabHost = getTabHost(); tabHost = getTabHost();
createTabs(); createTabs();
Intent i = getIntent(); Intent i = getIntent();
if (i.hasExtra("uri")) { if (i.hasExtra("uri")) {
Intent call = new Intent(this, ManageRepo.class); Intent call = new Intent(this, ManageRepo.class);
call.putExtra("uri", i.getStringExtra("uri")); call.putExtra("uri", i.getStringExtra("uri"));
startActivityForResult(call, REQUEST_MANAGEREPOS); startActivityForResult(call, REQUEST_MANAGEREPOS);
} }
} }
@Override @Override
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
((FDroidApp) getApplication()).inActivity++; ((FDroidApp) getApplication()).inActivity++;
db = new DB(this); db = new DB(this);
triedEmptyUpdate=false; triedEmptyUpdate = false;
populateLists(true); populateLists(true);
} }
@Override @Override
protected void onStop() { protected void onStop() {
db.close(); db.close();
((FDroidApp) getApplication()).inActivity--; ((FDroidApp) getApplication()).inActivity--;
super.onStop(); super.onStop();
} }
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu); super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE, UPDATE_REPO, 1, R.string.menu_update_repo).setIcon( menu.add(Menu.NONE, UPDATE_REPO, 1, R.string.menu_update_repo).setIcon(
android.R.drawable.ic_menu_rotate); android.R.drawable.ic_menu_rotate);
menu.add(Menu.NONE, MANAGE_REPO, 2, R.string.menu_manage).setIcon( menu.add(Menu.NONE, MANAGE_REPO, 2, R.string.menu_manage).setIcon(
android.R.drawable.ic_menu_agenda); android.R.drawable.ic_menu_agenda);
menu.add(Menu.NONE, PREFERENCES, 3, R.string.menu_preferences).setIcon( menu.add(Menu.NONE, PREFERENCES, 3, R.string.menu_preferences).setIcon(
android.R.drawable.ic_menu_preferences); android.R.drawable.ic_menu_preferences);
menu.add(Menu.NONE, ABOUT, 4, R.string.menu_about).setIcon( menu.add(Menu.NONE, ABOUT, 4, R.string.menu_about).setIcon(
android.R.drawable.ic_menu_help); android.R.drawable.ic_menu_help);
return true; return true;
} }
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case UPDATE_REPO: case UPDATE_REPO:
updateRepos(); updateRepos();
return true; return true;
case MANAGE_REPO: case MANAGE_REPO:
Intent i = new Intent(this, ManageRepo.class); Intent i = new Intent(this, ManageRepo.class);
startActivityForResult(i, REQUEST_MANAGEREPOS); startActivityForResult(i, REQUEST_MANAGEREPOS);
return true; return true;
case PREFERENCES: case PREFERENCES:
Intent prefs = new Intent(getBaseContext(), Preferences.class); Intent prefs = new Intent(getBaseContext(), Preferences.class);
startActivityForResult(prefs, REQUEST_PREFS); startActivityForResult(prefs, REQUEST_PREFS);
return true; return true;
case ABOUT: case ABOUT:
LayoutInflater li = LayoutInflater.from(this); LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.about, null); View view = li.inflate(R.layout.about, null);
// Fill in the version... // Fill in the version...
TextView tv = (TextView) view.findViewById(R.id.version); TextView tv = (TextView) view.findViewById(R.id.version);
PackageManager pm = getPackageManager(); PackageManager pm = getPackageManager();
PackageInfo pi; PackageInfo pi;
try { try {
pi = pm.getPackageInfo( pi = pm.getPackageInfo(
getApplicationContext().getPackageName(), 0); getApplicationContext().getPackageName(), 0);
tv.setText(pi.versionName); tv.setText(pi.versionName);
} catch (Exception e) { } catch (Exception e) {
} }
Builder p = new AlertDialog.Builder(this).setView(view); Builder p = new AlertDialog.Builder(this).setView(view);
final AlertDialog alrt = p.create(); final AlertDialog alrt = p.create();
alrt.setIcon(R.drawable.icon); alrt.setIcon(R.drawable.icon);
alrt.setTitle(getString(R.string.about_title)); alrt.setTitle(getString(R.string.about_title));
alrt.setButton(AlertDialog.BUTTON_NEUTRAL, alrt.setButton(AlertDialog.BUTTON_NEUTRAL,
getString(R.string.about_website), getString(R.string.about_website),
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, public void onClick(DialogInterface dialog,
int whichButton) { int whichButton) {
Uri uri = Uri.parse("http://f-droid.org"); Uri uri = Uri.parse("http://f-droid.org");
startActivity(new Intent(Intent.ACTION_VIEW, uri)); startActivity(new Intent(Intent.ACTION_VIEW, uri));
} }
}); });
alrt.setButton(AlertDialog.BUTTON_NEGATIVE, getString(R.string.ok), alrt.setButton(AlertDialog.BUTTON_NEGATIVE, getString(R.string.ok),
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, public void onClick(DialogInterface dialog,
int whichButton) { int whichButton) {
} }
}); });
alrt.show(); alrt.show();
return true; return true;
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) { switch (requestCode) {
case REQUEST_APPDETAILS: case REQUEST_APPDETAILS:
break; break;
case REQUEST_MANAGEREPOS: case REQUEST_MANAGEREPOS:
if (data.hasExtra("update")) { if (data.hasExtra("update")) {
AlertDialog.Builder ask_alrt = new AlertDialog.Builder(this); AlertDialog.Builder ask_alrt = new AlertDialog.Builder(this);
ask_alrt.setTitle(getString(R.string.repo_update_title)); ask_alrt.setTitle(getString(R.string.repo_update_title));
ask_alrt.setIcon(android.R.drawable.ic_menu_rotate); ask_alrt.setIcon(android.R.drawable.ic_menu_rotate);
ask_alrt.setMessage(getString(R.string.repo_alrt)); ask_alrt.setMessage(getString(R.string.repo_alrt));
ask_alrt.setPositiveButton(getString(R.string.yes), ask_alrt.setPositiveButton(getString(R.string.yes),
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, public void onClick(DialogInterface dialog,
int whichButton) { int whichButton) {
updateRepos(); updateRepos();
} }
}); });
ask_alrt.setNegativeButton(getString(R.string.no), ask_alrt.setNegativeButton(getString(R.string.no),
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, public void onClick(DialogInterface dialog,
int whichButton) { int whichButton) {
return; return;
} }
}); });
AlertDialog alert = ask_alrt.create(); AlertDialog alert = ask_alrt.create();
alert.show(); alert.show();
} }
break; break;
case REQUEST_PREFS: case REQUEST_PREFS:
// The automatic update settings may have changed, so reschedule (or // The automatic update settings may have changed, so reschedule (or
// unschedule) the // unschedule) the
// service accordingly. It's cheap, so no need to check if the // service accordingly. It's cheap, so no need to check if the
// particular setting has // particular setting has
// actually been changed. // actually been changed.
UpdateService.schedule(getBaseContext()); UpdateService.schedule(getBaseContext());
break; break;
} }
} }
private void createTabs() { private void createTabs() {
tabHost.clearAllTabs(); tabHost.clearAllTabs();
// TabContentFactory that can generate the appropriate list for each // TabContentFactory that can generate the appropriate list for each
// tab... // tab...
TabHost.TabContentFactory tf = new TabHost.TabContentFactory() { TabHost.TabContentFactory tf = new TabHost.TabContentFactory() {
@Override @Override
public View createTabContent(String tag) { public View createTabContent(String tag) {
AppListAdapter ad; AppListAdapter ad;
if (tag.equals(TAB_IN)) if (tag.equals(TAB_IN))
ad = apps_in; ad = apps_in;
else if (tag.equals(TAB_UP)) else if (tag.equals(TAB_UP))
ad = apps_up; ad = apps_up;
else else
ad = apps_av; ad = apps_av;
ListView lst = new ListView(FDroid.this); ListView lst = new ListView(FDroid.this);
lst.setOnItemClickListener(FDroid.this); lst.setOnItemClickListener(FDroid.this);
lst.setAdapter(ad); lst.setAdapter(ad);
return lst; return lst;
} }
}; };
// Create the tab of installed apps... // Create the tab of installed apps...
ts = tabHost.newTabSpec(TAB_IN); ts = tabHost.newTabSpec(TAB_IN);
ts.setIndicator(getString(R.string.tab_installed), getResources() ts.setIndicator(getString(R.string.tab_installed), getResources()
.getDrawable(drawable.star_off)); .getDrawable(drawable.star_off));
ts.setContent(tf); ts.setContent(tf);
// Create the tab of apps with updates... // Create the tab of apps with updates...
tsUp = tabHost.newTabSpec(TAB_UP); tsUp = tabHost.newTabSpec(TAB_UP);
tsUp.setIndicator(getString(R.string.tab_updates), getResources() tsUp.setIndicator(getString(R.string.tab_updates), getResources()
.getDrawable(drawable.star_on)); .getDrawable(drawable.star_on));
tsUp.setContent(tf); tsUp.setContent(tf);
// Create the tab of available apps... // Create the tab of available apps...
ts1 = tabHost.newTabSpec(TAB_UN); ts1 = tabHost.newTabSpec(TAB_UN);
ts1.setIndicator(getString(R.string.tab_noninstalled), getResources() ts1.setIndicator(getString(R.string.tab_noninstalled), getResources()
.getDrawable(drawable.ic_input_add)); .getDrawable(drawable.ic_input_add));
ts1.setContent(tf); ts1.setContent(tf);
tabHost.addTab(ts1); tabHost.addTab(ts1);
tabHost.addTab(ts); tabHost.addTab(ts);
tabHost.addTab(tsUp); tabHost.addTab(tsUp);
} }
// Populate the lists. // Populate the lists.
// 'update' - true to update the installed status of the applications // 'update' - true to update the installed status of the applications
// by asking the system. // by asking the system.
private void populateLists(boolean update) { private void populateLists(boolean update) {
apps_in.clear(); apps_in.clear();
apps_av.clear(); apps_av.clear();
apps_up.clear(); apps_up.clear();
Vector<DB.App> apps = db.getApps(null, null, update); Vector<DB.App> apps = db.getApps(null, null, update);
if(apps.isEmpty()) { if (apps.isEmpty()) {
// Don't attempt this more than once - we may have invalid repositories. // Don't attempt this more than once - we may have invalid
if(triedEmptyUpdate) // repositories.
return; if (triedEmptyUpdate)
// If there are no apps, update from the repos - it must be a return;
// new installation. // If there are no apps, update from the repos - it must be a
Log.d("FDroid", "Empty app list forces repo update"); // new installation.
updateRepos(); Log.d("FDroid", "Empty app list forces repo update");
triedEmptyUpdate = true; updateRepos();
return; triedEmptyUpdate = true;
} return;
Log.d("FDroid", "Updating lists - " + apps.size() + " apps in total"); }
Log.d("FDroid", "Updating lists - " + apps.size() + " apps in total");
for (DB.App app : apps) {
if (app.installedVersion == null) { for (DB.App app : apps) {
apps_av.addItem(app); if (app.installedVersion == null) {
} else { apps_av.addItem(app);
apps_in.addItem(app); } else {
if (app.hasUpdates) apps_in.addItem(app);
apps_up.addItem(app); if (app.hasUpdates)
} apps_up.addItem(app);
} }
}
// Tell the lists that the data behind the adapter has changed, so
// they can refresh... // Update the count on the 'Updates' tab to show the number available.
apps_av.notifyDataSetChanged(); // This is quite unpleasant, but seems to be the only way to do it.
apps_in.notifyDataSetChanged(); TextView uptext = (TextView) tabHost.getTabWidget().getChildAt(2)
apps_up.notifyDataSetChanged(); .findViewById(android.R.id.title);
uptext.setText(getString(R.string.tab_updates) + " ("
} + Integer.toString(apps_up.getCount()) + ")");
public boolean updateRepos() { // Tell the lists that the data behind the adapter has changed, so
pd = ProgressDialog.show(this, getString(R.string.process_wait_title), // they can refresh...
getString(R.string.process_update_msg), true); apps_av.notifyDataSetChanged();
pd.setIcon(android.R.drawable.ic_dialog_info); apps_in.notifyDataSetChanged();
apps_up.notifyDataSetChanged();
// Check for connection first!
ConnectivityManager netstate = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); }
if (netstate.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED
|| netstate.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED) { public boolean updateRepos() {
new Thread() { pd = ProgressDialog.show(this, getString(R.string.process_wait_title),
public void run() { getString(R.string.process_update_msg), true);
RepoXMLHandler.doUpdates(FDroid.this, db); pd.setIcon(android.R.drawable.ic_dialog_info);
update_handler.sendEmptyMessage(0);
} // Check for connection first!
}.start(); ConnectivityManager netstate = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return true; if (netstate.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED
} else { || netstate.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED) {
pd.dismiss(); new Thread() {
Toast.makeText(FDroid.this, getString(R.string.connection_error), public void run() {
Toast.LENGTH_LONG).show(); RepoXMLHandler.doUpdates(FDroid.this, db);
return false; update_handler.sendEmptyMessage(0);
} }
} }.start();
return true;
/* } else {
* Handlers for thread functions that need to access GUI pd.dismiss();
*/ Toast.makeText(FDroid.this, getString(R.string.connection_error),
private Handler update_handler = new Handler() { Toast.LENGTH_LONG).show();
@Override return false;
public void handleMessage(Message msg) { }
populateLists(true); }
if (pd.isShowing())
pd.dismiss(); /*
} * Handlers for thread functions that need to access GUI
}; */
private Handler update_handler = new Handler() {
// Handler for a click on one of the items in an application list. Pops @Override
// up a dialog that shows the details of the application and all its public void handleMessage(Message msg) {
// available versions, with buttons to allow installation etc. populateLists(true);
public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, if (pd.isShowing())
long arg3) { pd.dismiss();
}
final DB.App app; };
String curtab = tabHost.getCurrentTabTag();
if (curtab.equalsIgnoreCase(TAB_IN)) { // Handler for a click on one of the items in an application list. Pops
app = (DB.App) apps_in.getItem(arg2); // up a dialog that shows the details of the application and all its
} else if (curtab.equalsIgnoreCase(TAB_UP)) { // available versions, with buttons to allow installation etc.
app = (DB.App) apps_up.getItem(arg2); public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,
} else { long arg3) {
app = (DB.App) apps_av.getItem(arg2);
} final DB.App app;
String curtab = tabHost.getCurrentTabTag();
Intent intent = new Intent(this, AppDetails.class); if (curtab.equalsIgnoreCase(TAB_IN)) {
intent.putExtra("appid", app.id); app = (DB.App) apps_in.getItem(arg2);
startActivityForResult(intent, REQUEST_APPDETAILS); } else if (curtab.equalsIgnoreCase(TAB_UP)) {
app = (DB.App) apps_up.getItem(arg2);
} } else {
app = (DB.App) apps_av.getItem(arg2);
}
Intent intent = new Intent(this, AppDetails.class);
intent.putExtra("appid", app.id);
startActivityForResult(intent, REQUEST_APPDETAILS);
}
} }