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

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.logging.Logger;

import org.deviceconnect.android.manager.hmac.HmacManager;
import org.deviceconnect.android.manager.setting.SettingActivity;
import org.deviceconnect.message.intent.message.IntentDConnectMessage;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Device Connect Manager Launch Activity.
 * 
 * @author NTT DOCOMO, INC.
 */
public class DConnectLaunchActivity extends Activity {

    /** The name of URI scheme for launching Device Connect Manager. */
    private static final String SCHEME_LAUNCH = "dconnect";

    /** The HMAC manager. */
    private HmacManager mHmacManager;

    /** The logger. */
    protected final Logger mLogger = Logger.getLogger("dconnect.manager");

    /** The button to launch or stop Device Connect Manager. */
    private Button mLaunchOrStopButton;

    /** The text view to show a prompt message. */
    private TextView mMessageView;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dconnect_launcher);
        mHmacManager = new HmacManager(this);

        mMessageView = (TextView) findViewById(R.id.text_manager_launcher_message);
        mLaunchOrStopButton = (Button) findViewById(R.id.button_manager_launcher_launch_or_stop);
        Button cancelButton = (Button) findViewById(R.id.button_manager_launcher_cancel);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                finish();
            }
        });
    }

    @Override
    protected void onNewIntent(final Intent intent) {
        setIntent(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        toggleButton(isDConnectServiceRunning());

        Intent intent = getIntent();
        if (intent != null && SCHEME_LAUNCH.equals(intent.getScheme())) {
            String key = intent.getStringExtra(IntentDConnectMessage.EXTRA_KEY);
            String origin = intent.getStringExtra(IntentDConnectMessage.EXTRA_ORIGIN);
            mLogger.info("Requested to update HMAC key: origin=" + origin + ", key=" + key);
            try {
                if (origin != null) {
                    origin = URLDecoder.decode(origin, "UTF-8");
                    if (key != null && !TextUtils.isEmpty(origin)) {
                        mHmacManager.updateKey(origin, key);
                    }
                }
            } catch (UnsupportedEncodingException e) {
                // nothing to do.
                mLogger.warning("Failed to decode origin=" + origin);
                return;
            }
        }
    }

    /**
     * Toggles the text on views.
     * 
     * @param isLaunched <code>true</code> if Device Connect Manager is running, otherwise <code>false</code>
     */
    private void toggleButton(final boolean isLaunched) {
        if (isLaunched) {
            mMessageView.setText(R.string.activity_launch_message_stop);
            mLaunchOrStopButton.setText(R.string.activity_launch_button_stop);
            mLaunchOrStopButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    stopService(new Intent(DConnectLaunchActivity.this, DConnectService.class));
                    finish();
                }
            });
        } else {
            mMessageView.setText(R.string.activity_launch_message_launch);
            mLaunchOrStopButton.setText(R.string.activity_launch_button_launch);
            mLaunchOrStopButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    startService(new Intent(DConnectLaunchActivity.this, DConnectService.class));
                    finish();
                }
            });
        }
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        getMenuInflater().inflate(R.menu.activity_dconnect_launcher, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.dconnect_launcher_menu_item_settings) {
            Intent intent = new Intent(this, SettingActivity.class);
            startActivity(intent);
        } else if (id == android.R.id.home) {
            onBackPressed();
        }
        return true;
    }

    /**
     * Checks whether {@link DConnectService} is running or not.
     * @return <code>true</code> if {@link DConnectService} is running, otherwise <code>false</code>.
     */
    private boolean isDConnectServiceRunning() {
        return isServiceRunning(this, DConnectService.class);
    }

    /**
     * Checks whether the specified service is running or not.
     * @param c an instance of {@link Context}
     * @param cls the class of the specified service.
     * @return <code>true</code> if the specified service is running, otherwise <code>false</code>.
     */
    private boolean isServiceRunning(final Context c, final Class<?> cls) {
        ActivityManager am = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningServiceInfo> runningService = am.getRunningServices(Integer.MAX_VALUE);
        for (RunningServiceInfo i : runningService) {
            if (cls.getName().equals(i.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

}

@@ -699,16 +699,40 @@
 Intent;%0A
+import android.net.Uri;%0A
 import a
@@ -974,17 +974,16 @@
 vity.%0A *
- 
 %0A * @aut
@@ -2812,16 +2812,28 @@
 HMAC key
+ from intent
 : origin
@@ -2859,24 +2859,528 @@
 y=%22 + key);%0A
+%0A            if (key == null %7C%7C origin == null) %7B%0A                mLogger.warning(%22Origin or key is missing.%22);%0A                Uri uri = intent.getData();%0A                if (uri != null) %7B%0A                    key = uri.getQueryParameter(IntentDConnectMessage.EXTRA_KEY);%0A                    origin = uri.getQueryParameter(IntentDConnectMessage.EXTRA_ORIGIN);%0A                    mLogger.info(%22Requested to update HMAC key from URI: origin=%22 + origin + %22, key=%22 + key);%0A                %7D%0A            %7D%0A%0A
             
@@ -3920,17 +3920,16 @@
 .%0A     *
- 
 %0A     * 

Modify HMAC key reception.