4
* http://www.launchpad.net/tomdroid
6
* Copyright 2010, Rodja Trappe <mail@rodja.net>
8
* This file is part of Tomdroid.
10
* Tomdroid is free software: you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
15
* Tomdroid is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with Tomdroid. If not, see <http://www.gnu.org/licenses/>.
23
package org.tomdroid.ui;
25
import org.tomdroid.R;
26
import org.tomdroid.sync.SyncManager;
27
import org.tomdroid.sync.SyncService;
29
import android.app.Activity;
30
import android.app.AlertDialog;
31
import android.content.DialogInterface;
32
import android.content.DialogInterface.OnClickListener;
33
import android.os.Handler;
34
import android.os.Message;
35
import android.util.Log;
36
import android.view.View;
37
import android.view.animation.Animation;
38
import android.view.animation.AnimationUtils;
39
import android.view.animation.RotateAnimation;
40
import android.widget.ImageView;
41
import android.widget.Toast;
43
public class SyncMessageHandler extends Handler {
45
private static String TAG = "SycnMessageHandler";
46
private Activity activity;
49
private boolean parsingErrorShown = false;
51
public SyncMessageHandler(Activity activity) {
52
this.activity = activity;
56
public void handleMessage(Message msg) {
59
case SyncService.PARSING_COMPLETE:
60
// TODO put string in a translatable bundle
63
"Synchronization with "
64
+ SyncManager.getInstance().getCurrentService().getDescription()
65
+ " is complete.", Toast.LENGTH_SHORT).show();
68
case SyncService.PARSING_NO_NOTES:
69
// TODO put string in a translatable bundle
73
+ SyncManager.getInstance().getCurrentService().getDescription()
74
+ ".", Toast.LENGTH_SHORT).show();
77
case SyncService.PARSING_FAILED:
78
if (Tomdroid.LOGGING_ENABLED)
79
Log.w(TAG, "handler called with a parsing failed message");
81
// if we already shown a parsing error in this pass, we
82
// won't show it again
83
if (!parsingErrorShown) {
84
parsingErrorShown = true;
86
// TODO put error string in a translatable resource
87
new AlertDialog.Builder(activity).setMessage(
88
"There was an error trying to parse your note collection. If "
89
+ "you are able to replicate the problem, please contact us!")
90
.setTitle("Error").setNeutralButton("Ok", new OnClickListener() {
91
public void onClick(DialogInterface dialog, int which) {
98
case SyncService.NO_INTERNET:
99
// TODO put string in a translatable bundle
100
Toast.makeText(activity, "You are not connected to the internet.",
101
Toast.LENGTH_SHORT).show();
104
case SyncService.SYNC_PROGRESS:
105
handleSyncProgress(msg);
109
if (Tomdroid.LOGGING_ENABLED)
110
Log.i(TAG, "handler called with an unknown message");
116
private void handleSyncProgress(Message msg) {
117
ImageView syncIcon = (ImageView) activity.findViewById(R.id.syncIcon);
119
RotateAnimation rotation = new RotateAnimation(180 * msg.arg2 / 100f,
120
180 * msg.arg1 / 100f, Animation.RELATIVE_TO_SELF, 0.5f,
121
Animation.RELATIVE_TO_SELF, 0.5f);
122
rotation.setDuration(700);
123
rotation.setFillAfter(true);
124
syncIcon.startAnimation(rotation);
127
onSynchronizationStarted();
128
} else if (msg.arg1 == 100) {
129
onSynchronizationDone();
133
private void onSynchronizationDone() {
134
ImageView syncButton = (ImageView) activity.findViewById(R.id.sync);
135
ImageView syncIcon = (ImageView) activity.findViewById(R.id.syncIcon);
137
syncButton.setClickable(true);
138
syncIcon.getDrawable().setAlpha(Actionbar.DEFAULT_ICON_ALPHA);
140
View dot = activity.findViewById(R.id.sync_dot);
141
dot.setVisibility(View.INVISIBLE);
142
dot.getAnimation().setRepeatCount(0);
145
private void onSynchronizationStarted() {
146
ImageView syncButton = (ImageView) activity.findViewById(R.id.sync);
147
ImageView syncIcon = (ImageView) activity.findViewById(R.id.syncIcon);
149
syncButton.setClickable(false);
150
syncIcon.getDrawable().setAlpha(40);
152
Animation pulse = AnimationUtils.loadAnimation(activity, R.anim.pulse);
153
View dot = activity.findViewById(R.id.sync_dot);
154
dot.setVisibility(View.VISIBLE);
155
dot.startAnimation(pulse);