1/*
2 * Copyright (C) 2013 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.latin.personalization;
18
19import android.app.AlarmManager;
20import android.app.PendingIntent;
21import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24
25import java.util.concurrent.TimeUnit;
26
27/**
28 * Broadcast receiver for periodically updating decaying dictionaries.
29 */
30public class DictionaryDecayBroadcastReciever extends BroadcastReceiver {
31    /**
32     * The root domain for the personalization.
33     */
34    private static final String PERSONALIZATION_DOMAIN =
35            "com.android.inputmethod.latin.personalization";
36
37    /**
38     * The action of the intent to tell the time to decay dictionaries.
39     */
40    private static final String DICTIONARY_DECAY_INTENT_ACTION =
41            PERSONALIZATION_DOMAIN + ".DICT_DECAY";
42
43    /**
44     * Interval to update for decaying dictionaries.
45     */
46    private static final long DICTIONARY_DECAY_INTERVAL = TimeUnit.MINUTES.toMillis(60);
47
48    public static void setUpIntervalAlarmForDictionaryDecaying(Context context) {
49        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
50        final Intent updateIntent = new Intent(DICTIONARY_DECAY_INTENT_ACTION);
51        updateIntent.setClass(context, DictionaryDecayBroadcastReciever.class);
52        final long alarmTime =  System.currentTimeMillis() + DICTIONARY_DECAY_INTERVAL;
53        final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0 /* requestCode */,
54                updateIntent, PendingIntent.FLAG_CANCEL_CURRENT);
55        if (null != alarmManager) alarmManager.setInexactRepeating(AlarmManager.RTC,
56                alarmTime, DICTIONARY_DECAY_INTERVAL, pendingIntent);
57    }
58
59    @Override
60    public void onReceive(final Context context, final Intent intent) {
61        final String action = intent.getAction();
62        if (action.equals(DICTIONARY_DECAY_INTENT_ACTION)) {
63            PersonalizationHelper.tryDecayingAllOpeningUserHistoryDictionary();
64        }
65    }
66}
67