~tomdroid-dev/tomdroid/rework-auth-error-handling

« back to all changes in this revision

Viewing changes to src/org/tomdroid/ui/SyncMessageHandler.java

  • Committer: Olivier Bilodeau
  • Date: 2010-10-08 03:27:28 UTC
  • mfrom: (185.1.56 sync-ui)
  • Revision ID: olivier@bottomlesspit.org-20101008032728-yq5q48wngtkp2h1u
Oversimplifying things: One way note sync and improved UI

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Tomdroid
 
3
 * Tomboy on Android
 
4
 * http://www.launchpad.net/tomdroid
 
5
 * 
 
6
 * Copyright 2010, Rodja Trappe <mail@rodja.net>
 
7
 * 
 
8
 * This file is part of Tomdroid.
 
9
 * 
 
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.
 
14
 * 
 
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.
 
19
 * 
 
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/>.
 
22
 */
 
23
package org.tomdroid.ui;
 
24
 
 
25
import org.tomdroid.R;
 
26
import org.tomdroid.sync.SyncManager;
 
27
import org.tomdroid.sync.SyncService;
 
28
 
 
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;
 
42
 
 
43
public class SyncMessageHandler extends Handler {
 
44
 
 
45
        private static String TAG = "SycnMessageHandler";
 
46
        private Activity activity;
 
47
 
 
48
        // State variables
 
49
        private boolean parsingErrorShown = false;
 
50
        
 
51
        public SyncMessageHandler(Activity activity) {
 
52
                this.activity = activity;
 
53
        }
 
54
 
 
55
        @Override
 
56
        public void handleMessage(Message msg) {
 
57
 
 
58
                switch (msg.what) {
 
59
                        case SyncService.PARSING_COMPLETE:
 
60
                                // TODO put string in a translatable bundle
 
61
                                Toast.makeText(
 
62
                                                activity,
 
63
                                                "Synchronization with "
 
64
                                                                + SyncManager.getInstance().getCurrentService().getDescription()
 
65
                                                                + " is complete.", Toast.LENGTH_SHORT).show();
 
66
                                break;
 
67
 
 
68
                        case SyncService.PARSING_NO_NOTES:
 
69
                                // TODO put string in a translatable bundle
 
70
                                Toast.makeText(
 
71
                                                activity,
 
72
                                                "No notes found on "
 
73
                                                                + SyncManager.getInstance().getCurrentService().getDescription()
 
74
                                                                + ".", Toast.LENGTH_SHORT).show();
 
75
                                break;
 
76
 
 
77
                        case SyncService.PARSING_FAILED:
 
78
                                if (Tomdroid.LOGGING_ENABLED)
 
79
                                        Log.w(TAG, "handler called with a parsing failed message");
 
80
 
 
81
                                // if we already shown a parsing error in this pass, we
 
82
                                // won't show it again
 
83
                                if (!parsingErrorShown) {
 
84
                                        parsingErrorShown = true;
 
85
 
 
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) {
 
92
                                                                        dialog.dismiss();
 
93
                                                                }
 
94
                                                        }).show();
 
95
                                }
 
96
                                break;
 
97
 
 
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();
 
102
                                break;
 
103
 
 
104
                        case SyncService.SYNC_PROGRESS:
 
105
                                handleSyncProgress(msg);
 
106
                                break;
 
107
 
 
108
                        default:
 
109
                                if (Tomdroid.LOGGING_ENABLED)
 
110
                                        Log.i(TAG, "handler called with an unknown message");
 
111
                                break;
 
112
 
 
113
                }
 
114
        }
 
115
 
 
116
        private void handleSyncProgress(Message msg) {
 
117
                ImageView syncIcon = (ImageView) activity.findViewById(R.id.syncIcon);
 
118
 
 
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);
 
125
 
 
126
                if (msg.arg1 == 0) {
 
127
                        onSynchronizationStarted();
 
128
                } else if (msg.arg1 == 100) {
 
129
                        onSynchronizationDone();
 
130
                }
 
131
        }
 
132
 
 
133
        private void onSynchronizationDone() {
 
134
                ImageView syncButton = (ImageView) activity.findViewById(R.id.sync);
 
135
                ImageView syncIcon = (ImageView) activity.findViewById(R.id.syncIcon);
 
136
 
 
137
                syncButton.setClickable(true);
 
138
                syncIcon.getDrawable().setAlpha(Actionbar.DEFAULT_ICON_ALPHA);
 
139
 
 
140
                View dot = activity.findViewById(R.id.sync_dot);
 
141
                dot.setVisibility(View.INVISIBLE);
 
142
                dot.getAnimation().setRepeatCount(0);
 
143
        }
 
144
 
 
145
        private void onSynchronizationStarted() {
 
146
                ImageView syncButton = (ImageView) activity.findViewById(R.id.sync);
 
147
                ImageView syncIcon = (ImageView) activity.findViewById(R.id.syncIcon);
 
148
 
 
149
                syncButton.setClickable(false);
 
150
                syncIcon.getDrawable().setAlpha(40);
 
151
                
 
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);
 
156
        }
 
157
 
 
158
}