/*
 HostDeviceApplication.java
 Copyright (c) 2014 NTT DOCOMO,INC.
 Released under the MIT license
 http://opensource.org/licenses/mit-license.php
 */
package org.deviceconnect.android.deviceplugin.host;

import org.deviceconnect.android.profile.BatteryProfile;
import org.deviceconnect.android.profile.KeyEventProfile;
import org.deviceconnect.message.DConnectMessage;
import org.deviceconnect.message.intent.message.IntentDConnectMessage;

import android.app.Application;
import android.content.Intent;
import android.os.Bundle;

/**
 * Host Device Plugin Application.
 * 
 * @author NTT DOCOMO, INC.
 */
public class HostDeviceApplication extends Application {

    /** KeyEvent profile onDown cache. */
    Bundle mOnDownCache = null;

    /** KeyEvent profile onUp cache. */
    Bundle mOnUpCache = null;

    /**
     * Get KeyEvent cache data.
     * 
     * @param attr Attribute.
     * @return KeyEvent cache data.
     */
    public Bundle getKeyEventCache(final String attr) {
        if (attr.equals(KeyEventProfile.ATTRIBUTE_ON_DOWN)) {
            return mOnDownCache;
        } else if (attr.equals(KeyEventProfile.ATTRIBUTE_ON_UP)) {
            return mOnUpCache;
        } else {
            return null;
        }
    }

    /**
     * Set KeyEvent data to cache.
     * 
     * @param attr Attribute.
     * @param keyeventData Touch data.
     */
    public void setKeyEventCache(final String attr, final Bundle keyeventData) {
        if (attr.equals(KeyEventProfile.ATTRIBUTE_ON_DOWN)) {
            mOnDownCache = keyeventData;
        } else if (attr.equals(KeyEventProfile.ATTRIBUTE_ON_UP)) {
            mOnUpCache = keyeventData;
        }
    }

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

        // start accept service
        Intent request = new Intent(IntentDConnectMessage.ACTION_PUT);
        request.setClass(this, HostDeviceProvider.class);
        request.putExtra(DConnectMessage.EXTRA_PROFILE, BatteryProfile.PROFILE_NAME);
        sendBroadcast(request);
    }

}

@@ -764,49 +764,327 @@
 e on
-Up cache. */%0A    Bundle mOnUpCache = null
+Down cache time. */%0A    static long mOnDownCacheTime = 0;%0A%0A    /** KeyEvent profile onUp cache. */%0A    Bundle mOnUpCache = null;%0A%0A    /** KeyEvent profile onUp cache time. */%0A    static long mOnUpCacheTime = 0;%0A%0A    /** KeyEvent profile cache retention time (mSec). */%0A    static final long CACHE_RETENTION_TIME = 10000
 ;%0A%0A 
@@ -1256,24 +1256,80 @@
 ing attr) %7B%0A
+        long lCurrentTime = System.currentTimeMillis();%0A
         if (
@@ -1394,126 +1394,410 @@
     
-return mOnDownCache;%0A        %7D else if (attr.equals(KeyEventProfile.ATTRIBUTE_ON_UP)) %7B%0A            return mOnUpCache;
+if (lCurrentTime - mOnDownCacheTime %3C= CACHE_RETENTION_TIME) %7B%0A                return mOnDownCache;%0A            %7D else %7B%0A                return null;%0A            %7D%0A        %7D else if (attr.equals(KeyEventProfile.ATTRIBUTE_ON_UP)) %7B%0A            if (lCurrentTime - mOnUpCacheTime %3C= CACHE_RETENTION_TIME) %7B%0A                return mOnUpCache;%0A            %7D else %7B%0A                return null;%0A            %7D
 %0A   
@@ -2057,24 +2057,80 @@
 ventData) %7B%0A
+        long lCurrentTime = System.currentTimeMillis();%0A
         if (
@@ -2212,32 +2212,77 @@
 = keyeventData;%0A
+            mOnDownCacheTime = lCurrentTime;%0A
         %7D else i
@@ -2363,32 +2363,75 @@
 = keyeventData;%0A
+            mOnUpCacheTime = lCurrentTime;%0A
         %7D%0A    %7D%0A

Add cache time management.