Java tutorial
/* HueFargment02 Copyright (c) 2014 NTT DOCOMO,INC. Released under the MIT license http://opensource.org/licenses/mit-license.php */ package org.deviceconnect.android.deviceplugin.hue.activity.fragment; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.philips.lighting.hue.sdk.PHAccessPoint; import com.philips.lighting.hue.sdk.PHMessageType; import com.philips.lighting.hue.sdk.PHSDKListener; import com.philips.lighting.model.PHBridge; import com.philips.lighting.model.PHHueError; import com.philips.lighting.model.PHHueParsingError; import org.deviceconnect.android.deviceplugin.hue.db.HueManager; import org.deviceconnect.android.deviceplugin.hue.R; import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; /** * Hue?(2). * */ public class HueFragment02 extends Fragment implements OnClickListener { /** ?. */ private PHAccessPoint mAccessPoint; /** ?TextView. */ private TextView mTextViewStatus; /** Howto?TextView. */ private TextView mTextViewHowTo; /** Button. */ private Button mButton; /** ImageView. */ private ImageView mImageView; /** ??Counter. */ private int mCount = 0; /** . */ private HueManager.HueState mHueStatus = HueManager.HueState.INIT; /** . */ private final ScheduledExecutorService mExecutor = Executors.newSingleThreadScheduledExecutor(); /** . */ private ScheduledFuture<?> mFuture; /** * hue?Notification??????. */ private PHSDKListener mListener = new PHSDKListener() { @Override public void onAuthenticationRequired(final PHAccessPoint accessPoint) { mHueStatus = HueManager.HueState.INIT; HueManager.INSTANCE.startPushlinkAuthentication(accessPoint); authenticateHueBridge(); } @Override public void onAccessPointsFound(final List<PHAccessPoint> accessPoint) { } @Override public void onCacheUpdated(final List<Integer> list, final PHBridge bridge) { } @Override public void onBridgeConnected(final PHBridge phBridge, final String s) { mHueStatus = HueManager.HueState.AUTHENTICATE_SUCCESS; successAuthorization(); } @Override public void onConnectionLost(final PHAccessPoint accessPoint) { } @Override public void onConnectionResumed(final PHBridge bridge) { } @Override public void onError(final int code, final String message) { if (code == PHMessageType.PUSHLINK_AUTHENTICATION_FAILED) { failAuthorization(); } else if (code == PHHueError.NO_CONNECTION || code == PHHueError.BRIDGE_NOT_RESPONDING) { showNotConnection(); } } @Override public void onParsingErrors(final List<PHHueParsingError> errors) { } }; /** * HueFragment02?. * * @param accessPoint ???? * @return HueFragment02 */ public static HueFragment02 newInstance(final PHAccessPoint accessPoint) { HueFragment02 fragment = new HueFragment02(); fragment.setPHAccessPoint(accessPoint); return fragment; } @Override public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.hue_fragment_02, container, false); // Mac????. TextView macTextView = (TextView) rootView.findViewById(R.id.text_mac); macTextView.setText(mAccessPoint.getMacAddress()); // IP????. TextView ipTextView = (TextView) rootView.findViewById(R.id.text_ip); ipTextView.setText(mAccessPoint.getIpAddress()); // ??. mTextViewStatus = (TextView) rootView.findViewById(R.id.textStatus); // . mTextViewHowTo = (TextView) rootView.findViewById(R.id.textHowto); // . mButton = (Button) rootView.findViewById(R.id.btnBridgeTouroku); mButton.setOnClickListener(this); mButton.setVisibility(View.GONE); // ?. mImageView = (ImageView) rootView.findViewById(R.id.iv01); return rootView; } @Override public void onResume() { super.onResume(); // ?(INIT)?. mHueStatus = HueManager.HueState.INIT; HueManager.INSTANCE.addSDKListener(mListener); // Hue??? startAuthenticate(); } @Override public void onPause() { HueManager.INSTANCE.stopPushlinkAuthentication(); HueManager.INSTANCE.removeSDKListener(mListener); stopAnimation(); super.onPause(); } @Override public void onClick(final View v) { if (mHueStatus == HueManager.HueState.AUTHENTICATE_SUCCESS) { FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.setCustomAnimations(R.anim.fragment_slide_right_enter, R.anim.fragment_slide_left_exit, R.anim.fragment_slide_left_enter, R.anim.fragment_slide_right_exit); transaction.replace(R.id.fragment_frame, HueFragment03.newInstance(mAccessPoint)); transaction.commit(); } else { mButton.setVisibility(View.INVISIBLE); startAuthenticate(); } } private void setPHAccessPoint(final PHAccessPoint accessPoint) { mAccessPoint = accessPoint; } /** * Hue????. */ private synchronized void startAnimation() { if (mFuture != null) { mFuture.cancel(false); } mFuture = mExecutor.scheduleAtFixedRate(new Runnable() { @Override public void run() { switch (mHueStatus) { case INIT: nextImage(); break; default: case NO_CONNECT: case AUTHENTICATE_FAILED: case AUTHENTICATE_SUCCESS: stopAnimation(); break; } } }, 1, 1, TimeUnit.SECONDS); } /** * Hue?????. */ private synchronized void stopAnimation() { if (mFuture != null) { mFuture.cancel(false); mFuture = null; } } /** * ??RunnableUI????. * @param run ?Runnable */ private void runOnUiThread(final Runnable run) { final Activity activity = getActivity(); if (activity != null) { activity.runOnUiThread(run); } } /** * ?????. */ private void nextImage() { runOnUiThread(new Runnable() { @Override public void run() { if (mCount == 0) { mImageView.setImageResource(R.drawable.img01); } else { mImageView.setImageResource(R.drawable.img02); } mCount++; mCount %= 2; } }); } /** * ?????. */ private void successAuthorization() { runOnUiThread(new Runnable() { @Override public void run() { mTextViewStatus.setText(R.string.frag02_authsuccess); mTextViewHowTo.setText(R.string.frag02_authsuccess_howto); mImageView.setImageResource(R.drawable.img05); mButton.setText(R.string.frag02_authsuccess_btn); mButton.setVisibility(View.VISIBLE); String message = getString(R.string.frag02_connected); Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show(); } }); } /** * ????. */ private void failAuthorization() { runOnUiThread(new Runnable() { @Override public void run() { stopAnimation(); mTextViewStatus.setText(R.string.frag02_failed); mTextViewHowTo.setText(""); mImageView.setImageResource(R.drawable.img01); mButton.setText(R.string.frag02_retry_btn); mButton.setVisibility(View.VISIBLE); } }); } /** * Hue?????. * <p> * ?Hue????????? * </p> */ private void startAuthenticate() { HueManager.INSTANCE.startAuthenticate(mAccessPoint, new HueManager.HueConnectionListener() { @Override public void onConnected() { mHueStatus = HueManager.HueState.AUTHENTICATE_SUCCESS; successAuthorization(); } @Override public void onNotConnected() { authenticateHueBridge(); } }); } /** * Hue??????. */ private void authenticateHueBridge() { runOnUiThread(new Runnable() { @Override public void run() { mTextViewStatus.setText(R.string.frag02_init); mTextViewHowTo.setText(R.string.frag02_init_howto); mButton.setText(R.string.frag02_retry_btn); mButton.setVisibility(View.INVISIBLE); startAnimation(); } }); } /** * Hue????????????. */ private void showNotConnection() { runOnUiThread(new Runnable() { @Override public void run() { new AlertDialog.Builder(getActivity()).setTitle(R.string.frag02_failed) .setMessage(R.string.hue_dialog_no_connect) .setPositiveButton(R.string.hue_dialog_ok, new DialogInterface.OnClickListener() { @Override public void onClick(final DialogInterface dialog, final int which) { moveFirstFragment(); } }).setCancelable(false).show(); } }); } /** * ??????. */ private void moveFirstFragment() { FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.setCustomAnimations(R.anim.fragment_slide_right_enter, R.anim.fragment_slide_left_exit, R.anim.fragment_slide_left_enter, R.anim.fragment_slide_right_exit); transaction.replace(R.id.fragment_frame, new HueFragment01()); transaction.commit(); } }