UploaderService.java revision 4436446e9b173aab17c6927bb78b0f236381f7d1
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.inputmethod.research;
18
19import android.app.AlarmManager;
20import android.app.IntentService;
21import android.content.Intent;
22import android.os.Bundle;
23
24import com.android.inputmethod.latin.define.ProductionFlag;
25
26/**
27 * Service to invoke the uploader.
28 *
29 * Can be regularly invoked, invoked on boot, etc.
30 */
31public final class UploaderService extends IntentService {
32    private static final String TAG = UploaderService.class.getSimpleName();
33    private static final boolean DEBUG = false && ProductionFlag.IS_EXPERIMENTAL_DEBUG;
34    public static final long RUN_INTERVAL = AlarmManager.INTERVAL_HOUR;
35    public static final String EXTRA_UPLOAD_UNCONDITIONALLY = UploaderService.class.getName()
36            + ".extra.UPLOAD_UNCONDITIONALLY";
37    protected static final int TIMEOUT_IN_MS = 1000 * 4;
38
39    public UploaderService() {
40        super("Research Uploader Service");
41    }
42
43    @Override
44    protected void onHandleIntent(final Intent intent) {
45        final Uploader uploader = new Uploader(this);
46        if (!uploader.isPossibleToUpload()) return;
47        if (isUploadingUnconditionally(intent.getExtras()) || uploader.isConvenientToUpload()) {
48            uploader.doUpload();
49        }
50    }
51
52    private boolean isUploadingUnconditionally(final Bundle bundle) {
53        if (bundle == null) return false;
54        if (bundle.containsKey(EXTRA_UPLOAD_UNCONDITIONALLY)) {
55            return bundle.getBoolean(EXTRA_UPLOAD_UNCONDITIONALLY);
56        }
57        return false;
58    }
59}
60