Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package nextrek.photoupload;

import java.util.ArrayList;

import nextrek.lib.HTTPHelper;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import nextrek.services.ServiceConnector;

public class UploadService extends Service {

    private static final String TAG = "PhotoUpload.Service";

    static ArrayList<Bundle> mUploads = new ArrayList<Bundle>();

    static Handler mHandler = new Handler();

    Runnable mRunnable = new Runnable() {

        @Override
        public void run() {

            if (!HTTPHelper.isNetworkAvailable(UploadService.this)) {
                noNetworkNotification();
                return;
            }

            Bundle bundle;
            synchronized (mUploads) {

                if (mUploads.size() == 0) {
                    return;
                }
                bundle = mUploads.get(0);
                mUploads.remove(0);
            }

            String filename = bundle.getString("photo");
            String pratica = bundle.getString("pratica");
            String descrizione = bundle.getString("descrizione");

            bundle.putString("work-progress", "started");
            mServiceConnector.sendProgress(bundle);

            Log.e(TAG, "[START] " + filename + " " + pratica + " " + descrizione);
            startNotification(pratica);
            
            boolean success = true;

            try {
                HTTPHelper.doUPLOAD("http://www.nextrek.net/piroux/upload.php", bundle, filename);
                //HTTPHelper.doUPLOAD("http://10.9.1.68/piroux/upload.php", bundle, filename);
            } catch (Exception e) {
                Log.e(TAG, "[ERR] " + filename + " " + e.toString());
                success = false;
            }

            if (!success) {
                // retry
                synchronized (mUploads) {
                    mUploads.add(bundle);
                }
            }
            
            stopNotification(pratica);

            Log.e(TAG, "[STOP] " + filename + " " + pratica + " " + descrizione);

            bundle.putString("work-progress", "finished");
            mServiceConnector.sendProgress(bundle);

            synchronized (mUploads) {
                if (HTTPHelper.isNetworkAvailable(UploadService.this)) {
                    if (mUploads.size() > 0) {
                        runQueueHandler();
                    }
                } else {
                    noNetworkNotification();
                }
            }
        }
    };

    ServiceConnector mServiceConnector = new ServiceConnector() {

        public void onHandleRequest(Bundle bundle) {

            bundle.putString("work-progress", "queued");
            synchronized (mUploads) {
                mUploads.add(bundle);
            }
            sendProgress(bundle);

            runQueueHandler();
        }

    };

    private synchronized void runQueueHandler() {
        synchronized (mHandler) {
            mHandler.removeCallbacks(mRunnable);
            mHandler.post(mRunnable);
        }
    }

    /** For showing and hiding our notification. */
    NotificationManager mNM;

    private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
/*
            String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
            boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
            NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
*/
            if (!noConnectivity) {
                // quindi c'è connettività
                runQueueHandler();
            }

        }
    };

    @Override
    public void onCreate() {
        super.onCreate();

        mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // Display a notification about us starting.
        showNotification();

        Toast.makeText(this, R.string.remote_service_started, Toast.LENGTH_SHORT).show();

        registerReceiver(mConnReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        // Cancel the persistent notification.
        mNM.cancel(R.string.remote_service_started);

        // Tell the user we stopped.
        Toast.makeText(this, R.string.remote_service_stopped, Toast.LENGTH_SHORT).show();
        
        unregisterReceiver(mConnReceiver);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    /**
     * When binding to the service, we return an interface to our messenger
     * for sending messages to the service.
     */
    @Override
    public IBinder onBind(Intent intent) {
        return mServiceConnector.getBinder();
    }

    /**
     * Show a notification while this service is running.
     */
    private void showNotification() {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(R.string.remote_service_started);

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, getText(R.string.remote_service_label), text, contentIntent);

        // Send the notification.
        // We use a string id because it is a unique number. We use it later to cancel.
        mNM.notify(R.string.remote_service_started, notification);
    }

    /**
     * Show a notification while this service is running.
     */
    private void startNotification(String pratica) {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(R.string.start_label);

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, pratica, text, contentIntent);

        // Send the notification.
        // We use a string id because it is a unique number. We use it later to cancel.
        mNM.notify(R.string.remote_service_started, notification);
    }

    /**
     * Show a notification while this service is running.
     */
    private void stopNotification(String pratica) {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(R.string.stop_label);

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, pratica, text, contentIntent);

        // Send the notification.
        // We use a string id because it is a unique number. We use it later to cancel.
        mNM.notify(R.string.remote_service_started, notification);
    }

    private void noNetworkNotification() {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(R.string.stop_label);

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, getText(R.string.no_network_label), text, contentIntent);

        // Send the notification.
        // We use a string id because it is a unique number. We use it later to cancel.
        mNM.notify(R.string.remote_service_started, notification);
    }
}

Commits for Nextrek/Android/PhotoUpload/src/nextrek/photoupload/UploadService.java

Diff revisions: vs.
Revision Author Commited Message
4 FMMortaroli picture FMMortaroli Fri 19 Apr, 2013 16:54:38 +0000