Skip to content

Commit

Permalink
Added widget to display the unread count for an account
Browse files Browse the repository at this point in the history
  • Loading branch information
cketti committed Feb 13, 2012
1 parent 328701e commit 29e1a68
Show file tree
Hide file tree
Showing 13 changed files with 513 additions and 12 deletions.
20 changes: 20 additions & 0 deletions AndroidManifest.xml
Expand Up @@ -391,5 +391,25 @@ otherwise it would make K-9 start at the wrong time
android:readPermission="com.fsck.k9.permission.READ_MESSAGES"
android:writePermission="com.fsck.k9.permission.DELETE_MESSAGES"
/>

<receiver
android:name=".provider.UnreadWidgetProvider"
android:label="@string/unread_widget_label"
android:icon="@drawable/icon">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/unread_widget_info" />
</receiver>
<activity android:name=".activity.UnreadWidgetConfiguration">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<activity
android:name=".activity.AccountList">
</activity>
</application>
</manifest>
9 changes: 9 additions & 0 deletions res/drawable/rounded_corners.xml
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<solid android:color="#a0000000"/>
<corners android:radius="7dp"/>

</shape>
9 changes: 9 additions & 0 deletions res/drawable/unread_count_background.xml
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<solid android:color="#ffcc0000"/>
<corners android:radius="17dp"/>

</shape>
26 changes: 26 additions & 0 deletions res/layout/account_list.xml
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

<LinearLayout
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_vertical|center_horizontal">

<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"/>

</LinearLayout>
</LinearLayout>
11 changes: 0 additions & 11 deletions res/layout/launcher_shortcuts.xml

This file was deleted.

52 changes: 52 additions & 0 deletions res/layout/unread_widget_layout.xml
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/unread_widget_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:orientation="vertical"
android:gravity="bottom|center_horizontal">

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon" />

<TextView
android:id="@+id/unread_count"
android:textSize="10sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:paddingTop="0.5dp"
android:paddingBottom="0.5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="@drawable/unread_count_background"
android:textColor="#ffffff"/>

</FrameLayout>

<TextView
android:id="@+id/account_name"
android:ellipsize="none"
android:textSize="11sp"
android:shadowColor="#000000"
android:singleLine="true"
android:paddingTop="1dp"
android:paddingBottom="1dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="2dp"
android:background="@drawable/rounded_corners"
android:textColor="#ffffff"/>

</LinearLayout>
2 changes: 2 additions & 0 deletions res/values/strings.xml
Expand Up @@ -1126,4 +1126,6 @@ Welcome to K-9 Mail setup. K-9 is an open source mail client for Android origin
<string name="manage_accounts_move_down_action">Move down</string>
<string name="manage_accounts_moving_message">Moving account...</string>

<string name="unread_widget_label">Unread count</string>
<string name="unread_widget_select_account">Show unread count for…</string>
</resources>
8 changes: 8 additions & 0 deletions res/xml/unread_widget_info.xml
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/unread_widget_layout"
android:minHeight="40dp"
android:minWidth="40dp"
android:configure="com.fsck.k9.activity.UnreadWidgetConfiguration"
android:updatePeriodMillis="0">
</appwidget-provider>
163 changes: 163 additions & 0 deletions src/com/fsck/k9/activity/AccountList.java
@@ -0,0 +1,163 @@
package com.fsck.k9.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.fsck.k9.Account;
import com.fsck.k9.FontSizes;
import com.fsck.k9.K9;
import com.fsck.k9.Preferences;
import com.fsck.k9.R;


/**
* Activity displaying the list of accounts.
*
* <p>
* Classes extending this abstract class have to provide an {@link #onAccountSelected(Account)}
* method to perform an action when an account is selected.
* </p>
*/
public abstract class AccountList extends K9ListActivity implements OnItemClickListener {
private FontSizes mFontSizes = K9.getFontSizes();

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

setResult(RESULT_CANCELED);

setContentView(R.layout.account_list);

ListView listView = getListView();
listView.setOnItemClickListener(this);
listView.setItemsCanFocus(false);
}

/**
* Reload list of accounts when this activity is resumed.
*/
@Override
public void onResume() {
super.onResume();
new LoadAccounts().execute();
}

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Account account = (Account) parent.getItemAtPosition(position);
onAccountSelected(account);
}

/**
* Create a new {@link AccountsAdapter} instance and assign it to the {@link ListView}.
*
* @param accounts
* An array of accounts to display.
*/
public void populateListView(Account[] accounts) {
AccountsAdapter adapter = new AccountsAdapter(accounts);
ListView listView = getListView();
listView.setAdapter(adapter);
listView.invalidate();
}

/**
* This method will be called when an account was selected.
*
* @param account
* The account the user selected.
*/
protected abstract void onAccountSelected(Account account);

class AccountsAdapter extends ArrayAdapter<Account> {
private Account[] mAccounts;

public AccountsAdapter(Account[] accounts) {
super(AccountList.this, 0, accounts);
mAccounts = accounts;
}

public Account[] getAccounts() {
return mAccounts;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Account account = getItem(position);

final View view;
if (convertView != null) {
view = convertView;
} else {
view = getLayoutInflater().inflate(R.layout.accounts_item, parent, false);
view.findViewById(R.id.active_icons).setVisibility(View.GONE);
view.findViewById(R.id.folders).setVisibility(View.GONE);
view.getBackground().setAlpha(0);
}

AccountViewHolder holder = (AccountViewHolder) view.getTag();
if (holder == null) {
holder = new AccountViewHolder();
holder.description = (TextView) view.findViewById(R.id.description);
holder.email = (TextView) view.findViewById(R.id.email);
holder.chip = view.findViewById(R.id.chip);

view.setTag(holder);
}

String description = account.getDescription();
if (account.getEmail().equals(description)) {
holder.email.setVisibility(View.GONE);
} else {
holder.email.setVisibility(View.VISIBLE);
holder.email.setText(account.getEmail());
}

if (description == null || description.length() == 0) {
description = account.getEmail();
}

holder.description.setText(description);

holder.chip.setBackgroundColor(account.getChipColor());
holder.chip.getBackground().setAlpha(255);

holder.description.setTextSize(TypedValue.COMPLEX_UNIT_SP,
mFontSizes.getAccountName());
holder.email.setTextSize(TypedValue.COMPLEX_UNIT_SP,
mFontSizes.getAccountDescription());

return view;
}

class AccountViewHolder {
public TextView description;
public TextView email;
public View chip;
}
}

/**
* Load accounts in a background thread
*/
class LoadAccounts extends AsyncTask<Void, Void, Account[]> {
@Override
protected Account[] doInBackground(Void... params) {
Account[] accounts = Preferences.getPreferences(getApplicationContext()).getAccounts();
return accounts;
}

@Override
protected void onPostExecute(Account[] accounts) {
populateListView(accounts);
}
}
}
2 changes: 1 addition & 1 deletion src/com/fsck/k9/activity/LauncherShortcuts.java
Expand Up @@ -40,7 +40,7 @@ public void onCreate(Bundle icicle) {
return;
}

setContentView(R.layout.launcher_shortcuts);
setContentView(R.layout.account_list);
ListView listView = getListView();
listView.setOnItemClickListener(this);
listView.setItemsCanFocus(false);
Expand Down

0 comments on commit 29e1a68

Please sign in to comment.