| 1 | /* |
| 2 | * Tomdroid |
| 3 | * Tomboy on Android |
| 4 | * http://www.launchpad.net/tomdroid |
| 5 | * |
| 6 | * Copyright 2010, Rodja Trappe <mail@rodja.net> |
| 7 | * Copyright 2010, Benoit Garret <benoit.garret_launchpad@gadz.org> |
| 8 | * Copyright 2010, Olivier Bilodeau <olivier@bottomlesspit.org> |
| 9 | * |
| 10 | * This file is part of Tomdroid. |
| 11 | * |
| 12 | * Tomdroid is free software: you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation, either version 3 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * Tomdroid is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with Tomdroid. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | package org.tomdroid.ui; |
| 26 | |
| 27 | import org.tomdroid.R; |
| 28 | import org.tomdroid.sync.SyncManager; |
| 29 | import org.tomdroid.sync.SyncService; |
| 30 | import org.tomdroid.util.ErrorList; |
| 31 | |
| 32 | import android.app.Activity; |
| 33 | import android.app.AlertDialog; |
| 34 | import android.content.DialogInterface; |
| 35 | import android.content.DialogInterface.OnClickListener; |
| 36 | import android.os.Handler; |
| 37 | import android.os.Message; |
| 38 | import android.util.Log; |
| 39 | import android.view.View; |
| 40 | import android.view.animation.Animation; |
| 41 | import android.view.animation.AnimationUtils; |
| 42 | import android.view.animation.RotateAnimation; |
| 43 | import android.widget.ImageView; |
| 44 | import android.widget.Toast; |
| 45 | |
| 46 | public class SyncMessageHandler extends Handler { |
| 47 | |
| 48 | private static String TAG = "SycnMessageHandler"; |
| 49 | private Activity activity; |
| 50 | |
| 51 | public SyncMessageHandler(Activity activity) { |
| 52 | this.activity = activity; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void handleMessage(Message msg) { |
| 57 | |
| 58 | String serviceDescription = SyncManager.getInstance().getCurrentService().getDescription(); |
| 59 | String message = ""; |
| 60 | |
| 61 | switch (msg.what) { |
| 62 | |
| 63 | case SyncService.PARSING_COMPLETE: |
| 64 | final ErrorList errors = (ErrorList)msg.obj; |
| 65 | if(errors.isEmpty()) { |
| 66 | message = this.activity.getString(R.string.messageSyncComplete); |
| 67 | message = String.format(message,serviceDescription); |
| 68 | Toast.makeText(activity, message, Toast.LENGTH_SHORT).show(); |
| 69 | } else { |
| 70 | |
| 71 | message = this.activity.getString(R.string.messageSyncError); |
| 72 | new AlertDialog.Builder(activity).setMessage(message) |
| 73 | .setTitle(this.activity.getString(R.string.error)) |
| 74 | .setPositiveButton("Save to sd card", new OnClickListener() { |
| 75 | public void onClick(DialogInterface dialog, int which) { |
| 76 | if(!errors.save()) { |
| 77 | // TODO put string in a translatable bundle |
| 78 | Toast.makeText(activity, "Could not save the errors, please check your SD card.", |
| 79 | Toast.LENGTH_SHORT).show(); |
| 80 | } |
| 81 | } |
| 82 | }) |
| 83 | .setNegativeButton("Close", new OnClickListener() { |
| 84 | public void onClick(DialogInterface dialog, int which) {} |
| 85 | }).show(); |
| 86 | } |
| 87 | break; |
| 88 | |
| 89 | case SyncService.PARSING_NO_NOTES: |
| 90 | message = this.activity.getString(R.string.messageSyncNoNote); |
| 91 | message = String.format(message,serviceDescription); |
| 92 | Toast.makeText(activity, message, Toast.LENGTH_SHORT).show(); |
| 93 | break; |
| 94 | |
| 95 | case SyncService.NO_INTERNET: |
| 96 | // TODO put string in a translatable bundle |
| 97 | Toast.makeText(activity, this.activity.getString(R.string.messageSyncNoConnection), |
| 98 | Toast.LENGTH_SHORT).show(); |
| 99 | break; |
| 100 | |
| 101 | case SyncService.NO_SD_CARD: |
| 102 | // TODO put string in a translatable bundle |
| 103 | Toast.makeText(activity, "SD card not found.", |
| 104 | Toast.LENGTH_SHORT).show(); |
| 105 | break; |
| 106 | |
| 107 | case SyncService.SYNC_PROGRESS: |
| 108 | handleSyncProgress(msg); |
| 109 | break; |
| 110 | |
| 111 | default: |
| 112 | if (Tomdroid.LOGGING_ENABLED) |
| 113 | Log.i(TAG, "handler called with an unknown message"); |
| 114 | break; |
| 115 | |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | private void handleSyncProgress(Message msg) { |
| 120 | ImageView syncIcon = (ImageView) activity.findViewById(R.id.syncIcon); |
| 121 | |
| 122 | RotateAnimation rotation = new RotateAnimation(180 * msg.arg2 / 100f, |
| 123 | 180 * msg.arg1 / 100f, Animation.RELATIVE_TO_SELF, 0.5f, |
| 124 | Animation.RELATIVE_TO_SELF, 0.5f); |
| 125 | rotation.setDuration(700); |
| 126 | rotation.setFillAfter(true); |
| 127 | syncIcon.startAnimation(rotation); |
| 128 | |
| 129 | if (msg.arg1 == 0) { |
| 130 | onSynchronizationStarted(); |
| 131 | } else if (msg.arg1 == 100) { |
| 132 | onSynchronizationDone(); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | private void onSynchronizationDone() { |
| 137 | ImageView syncButton = (ImageView) activity.findViewById(R.id.sync); |
| 138 | ImageView syncIcon = (ImageView) activity.findViewById(R.id.syncIcon); |
| 139 | |
| 140 | syncButton.setClickable(true); |
| 141 | syncIcon.getDrawable().setAlpha(Actionbar.DEFAULT_ICON_ALPHA); |
| 142 | |
| 143 | View dot = activity.findViewById(R.id.sync_dot); |
| 144 | dot.setVisibility(View.INVISIBLE); |
| 145 | dot.getAnimation().setRepeatCount(0); |
| 146 | } |
| 147 | |
| 148 | private void onSynchronizationStarted() { |
| 149 | ImageView syncButton = (ImageView) activity.findViewById(R.id.sync); |
| 150 | ImageView syncIcon = (ImageView) activity.findViewById(R.id.syncIcon); |
| 151 | |
| 152 | syncButton.setClickable(false); |
| 153 | syncIcon.getDrawable().setAlpha(40); |
| 154 | |
| 155 | Animation pulse = AnimationUtils.loadAnimation(activity, R.anim.pulse); |
| 156 | View dot = activity.findViewById(R.id.sync_dot); |
| 157 | dot.setVisibility(View.VISIBLE); |
| 158 | dot.startAnimation(pulse); |
| 159 | } |
| 160 | |
| 161 | } |