import android.app.Activity;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.TextView;@SuppressWarnings("unused")public class SyncStatusPanel extends RelativeLayout {private class SetIconUIThread implements Runnable {private int image;public SetIconUIThread() {}public void run() {if (image != 0) {sourceIconView.setImageResource(image);} else {sourceIconView.setImageResource(0);}}public void setIcon(int image) {this.image = image;}}private class SetStatusIconUIThread implements Runnable {private int image;public SetStatusIconUIThread() {}public void run() {if (image != 0) {statusIconView.setImageResource(image);} else {statusIconView.setImageResource(0);}}public void setIcon(int image) {this.image = image;}}private class SetStatusStringUIThread implements Runnable {private String text;public SetStatusStringUIThread() {}public void run() {statusTextView.setText(text);}public void setText(String value) {this.text = value;}}private class SetTitleUIThread implements Runnable {private String title;public SetTitleUIThread() {}public void run() {titleTextView.setText(title);}public void setTitle(String title) {this.title = title;}}protected static final int BOTTOM_PADDING = 8;private static final int DISABLED_TEXT_COLOR = 0xFF7F7F7F;private static final int ENABLED_TEXT_COLOR = 0xFF000000;protected static final int SOURCE_ICON_LEFT_PADDING = 4;protected static final int SOURCE_ICON_RIGHT_PADDING = 4;private static final int STATUS_FONT_SIZE = 12;protected static final int STATUS_ICON_R_PADDING = 4;protected static final int STATUS_L_PADDING = 12;private static final int TITLE_FONT_SIZE = 20;protected static final int TITLE_L_PADDING = 5;protected static final int TOP_PADDING = 8;protected Activity activity;protected SetIconUIThread setIconUIThread = new SetIconUIThread();protected SetStatusIconUIThread setStatusIconUIThread = new SetStatusIconUIThread();protected SetStatusStringUIThread setStatusStringUIThread = new SetStatusStringUIThread();protected SetTitleUIThread setTitleUIThread = new SetTitleUIThread();protected ImageView sourceIconView;protected ImageView statusIconView;protected TextView statusTextView;protected TextView titleTextView;// 每個元素的IDprotected final int D1 = 0X514001, D2 = 0X514002, D3 = 0X514003,D4 = 0X514004;public SyncStatusPanel(Activity activity) {super(activity);this.activity = activity;statusIconView = createStatusIcon(activity);sourceIconView = createSourceIcon(activity);titleTextView = createTitleText(activity);statusTextView = createStatusText(activity);RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);params1.addRule(RelativeLayout.CENTER_VERTICAL);params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);addView(sourceIconView, params1);RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);params2.addRule(RelativeLayout.CENTER_VERTICAL);params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);addView(statusIconView, params2);RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);params3.addRule(RelativeLayout.RIGHT_OF,sourceIconView.getId());params3.addRule(RelativeLayout.ALIGN_PARENT_TOP);params3.addRule(RelativeLayout.LEFT_OF, statusIconView.getId());addView(titleTextView, params3);RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);params4.addRule(RelativeLayout.RIGHT_OF,sourceIconView.getId());params4.addRule(RelativeLayout.BELOW, titleTextView.getId());params4.addRule(RelativeLayout.LEFT_OF, statusIconView.getId());addView(statusTextView, params4);setBackgroundResource(R.drawable.sync_status_panel);}protected int adaptSizeToDensity(int size) {return (int) (size * getContext().getResources().getDisplayMetrics().density);}protected ImageView createSourceIcon(Activity activity) {ImageView sourceIconView = new ImageView(activity);sourceIconView.setPadding(adaptSizeToDensity(SOURCE_ICON_LEFT_PADDING),adaptSizeToDensity(TOP_PADDING),adaptSizeToDensity(SOURCE_ICON_RIGHT_PADDING),adaptSizeToDensity(BOTTOM_PADDING));sourceIconView.setId(D1);return sourceIconView;}protected ImageView createStatusIcon(Activity activity) {ImageView statusIconView = new ImageView(activity);statusIconView.setAdjustViewBounds(true);statusIconView.setMaxHeight(adaptSizeToDensity(32));statusIconView.setMaxWidth(adaptSizeToDensity(32));statusIconView.setId(D2);return statusIconView;}protected TextView createStatusText(Activity activity) {TextView statusTextView = new TextView(activity);statusTextView.setPadding(adaptSizeToDensity(STATUS_L_PADDING),0, 0, 0);statusTextView.setTextSize(STATUS_FONT_SIZE);statusTextView.setTextColor(ENABLED_TEXT_COLOR);statusTextView.setBackgroundColor(android.R.color.black);statusTextView.setId(D3);return statusTextView;}protected TextView createTitleText(Activity activity) {TextView titleTextView = new TextView(activity);titleTextView.setTextSize(TITLE_FONT_SIZE);titleTextView.setPadding(adaptSizeToDensity(TITLE_L_PADDING),adaptSizeToDensity(TOP_PADDING), 0, 0);titleTextView.setTextColor(ENABLED_TEXT_COLOR); // COLORtitleTextView.setId(D4);return titleTextView;}public void setIcon(int image) {setIconUIThread.setIcon(image);activity.runOnUiThread(setIconUIThread);}public void setStatusIcon(int image) {setStatusIconUIThread.setIcon(image);activity.runOnUiThread(setStatusIconUIThread);}public void setStatusString(String value) {setStatusStringUIThread.setText(value);activity.runOnUiThread(setStatusStringUIThread);}public void setTitle(String value) {setTitleUIThread.setTitle(value);activity.runOnUiThread(setTitleUIThread);}}