Merge "Start cleaning up QuickResponse code" into jb-ub-mail-ur10

This commit is contained in:
Tony Mantler 2013-08-08 02:21:18 +00:00 committed by Android (Google) Code Review
commit fdb0fc712a
3 changed files with 73 additions and 113 deletions

View File

@ -14,7 +14,7 @@
limitations under the License. limitations under the License.
--> -->
<RelativeLayout <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/line_item" android:id="@+id/line_item"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -23,27 +23,14 @@
android:focusable="true" android:focusable="true"
android:padding="7dip" android:padding="7dip"
> >
<ImageView
android:id="@+id/delete_icon"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="?android:attr/listChoiceBackgroundIndicator"
android:focusable="true"
android:padding="7dip"
android:src="@drawable/ic_menu_trash_holo_light"
/>
<TextView <TextView
android:id="@+id/quick_response_text" android:id="@+id/quick_response_text"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/delete_icon"
android:layout_centerVertical="true" android:layout_centerVertical="true"
style="@style/accountSetupInfoText" style="@style/accountSetupInfoText"
android:ellipsize="end" android:ellipsize="end"
android:focusable="false" android:focusable="false"
android:maxLines="2" android:maxLines="2"
/> />
</RelativeLayout> </LinearLayout>

View File

@ -54,8 +54,7 @@ import android.widget.TextView;
* *
* <p>This fragment is run as a preference panel from AccountSettings.</p> * <p>This fragment is run as a preference panel from AccountSettings.</p>
*/ */
public class AccountSettingsEditQuickResponsesFragment extends Fragment public class AccountSettingsEditQuickResponsesFragment extends Fragment {
implements OnClickListener {
private ListView mQuickResponsesView; private ListView mQuickResponsesView;
private Account mAccount; private Account mAccount;
private Context mContext; private Context mContext;
@ -69,10 +68,9 @@ public class AccountSettingsEditQuickResponsesFragment extends Fragment
private static class ArrayAdapterWithButtons extends ArrayAdapter<QuickResponse> { private static class ArrayAdapterWithButtons extends ArrayAdapter<QuickResponse> {
private QuickResponse[] mQuickResponses; private QuickResponse[] mQuickResponses;
private final long mAccountId; private final long mAccountId;
private final Context mContext;
private final FragmentManager mFragmentManager; private final FragmentManager mFragmentManager;
private OnClickListener mOnEditListener = new OnClickListener() { private final OnClickListener mOnEditListener = new OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
QuickResponse quickResponse = (QuickResponse) (view.getTag()); QuickResponse quickResponse = (QuickResponse) (view.getTag());
@ -82,23 +80,6 @@ public class AccountSettingsEditQuickResponsesFragment extends Fragment
} }
}; };
private OnClickListener mOnDeleteListener = new OnClickListener() {
@Override
public void onClick(View view) {
final QuickResponse quickResponse = (QuickResponse) view.getTag();
// Delete the QuickResponse from the database. Content watchers used to
// update the ListView of QuickResponses upon deletion.
EmailAsyncTask.runAsyncParallel(new Runnable() {
@Override
public void run() {
EmailContent.delete(mContext, quickResponse.getBaseUri(),
quickResponse.getId());
}
});
}
};
private static final int resourceId = R.layout.quick_response_item; private static final int resourceId = R.layout.quick_response_item;
private static final int textViewId = R.id.quick_response_text; private static final int textViewId = R.id.quick_response_text;
@ -116,7 +97,6 @@ public class AccountSettingsEditQuickResponsesFragment extends Fragment
super(context, resourceId, textViewId, quickResponses); super(context, resourceId, textViewId, quickResponses);
mQuickResponses = quickResponses; mQuickResponses = quickResponses;
mAccountId = accountId; mAccountId = accountId;
mContext = context;
mFragmentManager = fragmentManager; mFragmentManager = fragmentManager;
} }
@ -126,10 +106,6 @@ public class AccountSettingsEditQuickResponsesFragment extends Fragment
convertView.setTag(mQuickResponses[position]); convertView.setTag(mQuickResponses[position]);
convertView.setOnClickListener(mOnEditListener); convertView.setOnClickListener(mOnEditListener);
ImageView deleteIcon = (ImageView) convertView.findViewById(R.id.delete_icon);
deleteIcon.setTag(mQuickResponses[position]);
deleteIcon.setOnClickListener(mOnDeleteListener);
return convertView; return convertView;
} }
} }
@ -241,13 +217,13 @@ public class AccountSettingsEditQuickResponsesFragment extends Fragment
if (Logging.DEBUG_LIFECYCLE && MailActivityEmail.DEBUG) { if (Logging.DEBUG_LIFECYCLE && MailActivityEmail.DEBUG) {
LogUtils.d(Logging.LOG_TAG, "AccountSettingsEditQuickResponsesFragment onCreateView"); LogUtils.d(Logging.LOG_TAG, "AccountSettingsEditQuickResponsesFragment onCreateView");
} }
int layoutId = R.layout.account_settings_edit_quick_responses_fragment; View view = inflater.inflate(R.layout.account_settings_edit_quick_responses_fragment,
View view = inflater.inflate(layoutId, container, false); container, false);
mContext = getActivity(); mContext = getActivity();
mQuickResponsesView = UiUtilities.getView(view, mQuickResponsesView = UiUtilities.getView(view,
R.id.account_settings_quick_responses_list); R.id.account_settings_quick_responses_list);
TextView emptyView = (TextView) TextView emptyView =
UiUtilities.getView(((ViewGroup) mQuickResponsesView.getParent()), R.id.empty_view); UiUtilities.getView(((ViewGroup) mQuickResponsesView.getParent()), R.id.empty_view);
mQuickResponsesView.setEmptyView(emptyView); mQuickResponsesView.setEmptyView(emptyView);
@ -265,7 +241,14 @@ public class AccountSettingsEditQuickResponsesFragment extends Fragment
} }
}); });
UiUtilities.getView(view, R.id.create_new).setOnClickListener(this); UiUtilities.getView(view, R.id.create_new).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditQuickResponseDialog
.newInstance(null, mAccount.mId)
.show(getFragmentManager(), null);
}
});
return view; return view;
} }
@ -275,16 +258,4 @@ public class AccountSettingsEditQuickResponsesFragment extends Fragment
mTaskTracker.cancellAllInterrupt(); mTaskTracker.cancellAllInterrupt();
super.onDestroy(); super.onDestroy();
} }
/**
* Implements OnClickListener
*/
@Override
public void onClick(View v) {
if (v.getId() == R.id.create_new) {
EditQuickResponseDialog
.newInstance(null, mAccount.mId)
.show(getFragmentManager(), null);
}
}
} }

View File

@ -38,8 +38,7 @@ import android.widget.EditText;
/** /**
* Dialog to edit the text of a given or new quick response * Dialog to edit the text of a given or new quick response
*/ */
public class EditQuickResponseDialog extends DialogFragment public class EditQuickResponseDialog extends DialogFragment {
implements DialogInterface.OnClickListener, TextWatcher {
private EditText mQuickResponseEditText; private EditText mQuickResponseEditText;
private QuickResponse mQuickResponse; private QuickResponse mQuickResponse;
private AlertDialog mDialog; private AlertDialog mDialog;
@ -73,7 +72,7 @@ public class EditQuickResponseDialog extends DialogFragment
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
final Context context = getActivity(); final Context context = getActivity();
mQuickResponse = (QuickResponse) getArguments().getParcelable(QUICK_RESPONSE); mQuickResponse = (QuickResponse) getArguments().getParcelable(QUICK_RESPONSE);
View wrapper = LayoutInflater.from(context) final View wrapper = LayoutInflater.from(context)
.inflate(R.layout.quick_response_edit_dialog, null); .inflate(R.layout.quick_response_edit_dialog, null);
mQuickResponseEditText = (EditText) wrapper.findViewById(R.id.quick_response_text); mQuickResponseEditText = (EditText) wrapper.findViewById(R.id.quick_response_text);
if (savedInstanceState != null) { if (savedInstanceState != null) {
@ -86,13 +85,66 @@ public class EditQuickResponseDialog extends DialogFragment
mQuickResponseEditText.setText(mQuickResponse.toString()); mQuickResponseEditText.setText(mQuickResponse.toString());
} }
mQuickResponseEditText.setSelection(mQuickResponseEditText.length()); mQuickResponseEditText.setSelection(mQuickResponseEditText.length());
mQuickResponseEditText.addTextChangedListener(this); mQuickResponseEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
mDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(s.length() > 0);
}
});
DialogInterface.OnClickListener saveClickListener =
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final long accountId = getArguments().getLong("accountId");
final String text = mQuickResponseEditText.getText().toString();
final Context context = getActivity();
if (mQuickResponse == null) {
mQuickResponse = new QuickResponse(accountId, text);
}
// Insert the new QuickResponse into the database. Content watchers used to
// update the ListView of QuickResponses upon insertion.
EmailAsyncTask.runAsyncParallel(new Runnable() {
@Override
public void run() {
if (!mQuickResponse.isSaved()) {
mQuickResponse.save(context);
} else {
ContentValues values = new ContentValues();
values.put(QuickResponseColumns.TEXT, text);
mQuickResponse.update(context, values);
}
}
});
}
};
DialogInterface.OnClickListener deleteClickListener =
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (mQuickResponse == null) {
return;
}
// TODO: confirm delete
mQuickResponse.delete(context, mQuickResponse.getBaseUri(),
mQuickResponse.getId());
}
};
final AlertDialog.Builder b = new AlertDialog.Builder(context); final AlertDialog.Builder b = new AlertDialog.Builder(context);
b.setTitle(getResources().getString(R.string.edit_quick_response_dialog)) b.setTitle(getResources().getString(R.string.edit_quick_response_dialog))
.setView(wrapper) .setView(wrapper)
.setNegativeButton(R.string.cancel_action, this) .setNegativeButton(R.string.cancel_action, null)
.setPositiveButton(R.string.save_action, this); .setPositiveButton(R.string.save_action, saveClickListener)
.setNeutralButton(R.string.delete, deleteClickListener);
mDialog = b.create(); mDialog = b.create();
return mDialog; return mDialog;
} }
@ -107,20 +159,6 @@ public class EditQuickResponseDialog extends DialogFragment
} }
} }
// implements TextWatcher
@Override
public void afterTextChanged(Editable s) {
mDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(s.length() > 0);
}
// implements TextWatcher
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
// implements TextWatcher
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
// Saves contents during orientation change // Saves contents during orientation change
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
@ -128,40 +166,4 @@ public class EditQuickResponseDialog extends DialogFragment
outState.putString( outState.putString(
QUICK_RESPONSE_EDITED_STRING, mQuickResponseEditText.getText().toString()); QUICK_RESPONSE_EDITED_STRING, mQuickResponseEditText.getText().toString());
} }
/**
* Implements DialogInterface.OnClickListener
*/
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_NEGATIVE:
dialog.cancel();
break;
case DialogInterface.BUTTON_POSITIVE:
final long accountId = getArguments().getLong("accountId");
final String text = mQuickResponseEditText.getText().toString();
final Context context = getActivity();
if (mQuickResponse == null) {
mQuickResponse = new QuickResponse(accountId, text);
}
// Insert the new QuickResponse into the database. Content watchers used to
// update the ListView of QuickResponses upon insertion.
EmailAsyncTask.runAsyncParallel(new Runnable() {
@Override
public void run() {
if (!mQuickResponse.isSaved()) {
mQuickResponse.save(context);
} else {
ContentValues values = new ContentValues();
values.put(QuickResponseColumns.TEXT, text);
mQuickResponse.update(context, values);
}
}
});
break;
}
}
} }