org.deviceconnect.android.deviceplugin.host.setting.HostSettingFragment.java Source code

Java tutorial

Introduction

Here is the source code for org.deviceconnect.android.deviceplugin.host.setting.HostSettingFragment.java

Source

/*
 HostSettingFragment.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.setting;

import static android.content.Context.WIFI_SERVICE;

import org.deviceconnect.android.deviceplugin.host.BuildConfig;
import org.deviceconnect.android.deviceplugin.host.HostDeviceService;
import org.deviceconnect.android.deviceplugin.host.IHostDeviceCallback;
import org.deviceconnect.android.deviceplugin.host.IHostDeviceService;
import org.deviceconnect.android.deviceplugin.host.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author NTT DOCOMO, INC.
 */
public class HostSettingFragment extends Fragment {

    /** Debug Tag. */
    private static final String TAG = "PluginHost";

    /** Host?IP????TextView. */
    private TextView mDeviceHostIpTextView;

    /** context. */
    Activity mActivity;

    /** PluginID. */
    String mPluginId;

    /** ?. */
    private static ProgressDialog mDialog;

    /** Handler Action. */
    private static final int HANDLER_ACTION_DISMISS = 1;

    /** ?????Service. */
    private static IHostDeviceService mService;

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
            final Bundle savedInstanceState) {

        // Activity?
        mActivity = this.getActivity();
        mActivity.bindService(new Intent(this.getActivity(), HostDeviceService.class), mServiceConnection,
                Context.BIND_AUTO_CREATE);

        // Position?
        Bundle mBundle = getArguments();
        int mPagePosition = mBundle.getInt("position", 0);

        int mPageLayoutId = this.getResources().getIdentifier("host_setting_" + mPagePosition, "layout",
                getActivity().getPackageName());

        View mView = inflater.inflate(mPageLayoutId, container, false);

        if (mPagePosition == 0) {
            WifiManager wifiManager = (WifiManager) this.getActivity().getSystemService(WIFI_SERVICE);
            int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
            final String formatedIpAddress = String.format("%d.%d.%d.%d", (ipAddress & 0xff),
                    (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));

            // Host IP
            mDeviceHostIpTextView = (TextView) mView.findViewById(R.id.host_ipaddress);
            mDeviceHostIpTextView.setText("Your IP:" + formatedIpAddress);
        }

        return mView;
    }

    @Override
    public void onPause() {
        super.onPause();
    }

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

    }

    /**
     * Host PluginSearch???.
     */
    public void searchHost() {

        showProgressDialog();
        try {
            mService.searchHost();
        } catch (RemoteException e) {
            if (BuildConfig.DEBUG) {
                e.printStackTrace();
            }
        }
        // Handler??
        MyHandler handler = new MyHandler();
        Message mMsg = new Message();
        mMsg.what = HANDLER_ACTION_DISMISS;
        handler.sendMessageDelayed(mMsg, 8000);
    }

    /**
     * Host PluginInvoke???.
     */
    public void invokeHost() {

        showProgressDialog();
        try {
            mService.invokeHost();
        } catch (RemoteException e) {
            if (BuildConfig.DEBUG) {
                e.printStackTrace();
            }
        }
        dismissProgressDialog();
    }

    /**
     * ???.
     * 
     * @return 
     */
    public Context getContext() {
        return mActivity;
    }

    /**
     * ??????.
     * 
     * @return ?????true,???false
     */
    public boolean isShowProgressDialog() {
        return mDialog != null;
    }

    /**
     * ??.
     */
    public void showProgressDialog() {
        if (mDialog != null) {
            return;
        }
        mDialog = new ProgressDialog(getActivity());
        mDialog.setTitle("?");
        mDialog.setMessage("Now Loading...");
        mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mDialog.setCancelable(false);
        mDialog.show();
    }

    /**
     * ????.
     */
    public void dismissProgressDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    /**
     * Handler??.
     */
    class MyHandler extends Handler {

        @Override
        public void handleMessage(final Message msg) {

            if (msg.what == HANDLER_ACTION_DISMISS) {
                dismissProgressDialog();

                Toast.makeText(mActivity,
                        "Host??????(:, :????Hosts????)",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * ?.
     */
    private ServiceConnection mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(final ComponentName name, final IBinder service) {
            Log.i(TAG, "onServiceConnected");
            mService = IHostDeviceService.Stub.asInterface(service);
            try {
                mService.registerCallback(mCallback);
            } catch (RemoteException e) {
                if (BuildConfig.DEBUG) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onServiceDisconnected(final ComponentName name) {
            mService = null;
            try {
                mService.unregisterCallback(mCallback);
            } catch (RemoteException e) {
                if (BuildConfig.DEBUG) {
                    e.printStackTrace();
                }
            }
        }
    };

    /**
     * ?Callback.
     */
    private IHostDeviceCallback mCallback = new IHostDeviceCallback.Stub() {

        @Override
        public void changeHostStatus(final int status) throws RemoteException {
            if (status == 1) {

                dismissProgressDialog();

                Looper.prepare();
                Toast.makeText(mActivity, "Host????", Toast.LENGTH_SHORT).show();
                Looper.loop();

            } else if (status == -1) {

                dismissProgressDialog();

                Looper.prepare();
                Toast.makeText(mActivity,
                        "Host??????(:, :????IrKit????)",
                        Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
        }

        @Override
        public void invokeHost(final String ipaddress) throws RemoteException {

            // Handler??
            mActivity.runOnUiThread(new Runnable() {
                public void run() {
                }
            });
        }

        @Override
        public void findHost(final String ipaddress) throws RemoteException {
            // Handler??
            mActivity.runOnUiThread(new Runnable() {
                public void run() {
                    // ????UI??
                    mDeviceHostIpTextView.setText("Find:" + ipaddress);
                }
            });
        }
    };
}