Java tutorial
/* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.mstar.tv.tvplayer.philips.option; import com.mstar.philips.network.NetSettingActivity; import com.mstar.philips.network.NetWorkStatusDialog; import com.mstar.tv.tvplayer.philips.setting.TvSettingMenuActivity; import com.mstar.tv.tvplayer.ui.R; import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.DialogInterface.OnDismissListener; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.net.wifi.p2p.WifiP2pManager; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ListView; import android.widget.Toast; public class OptionFirstFragment extends ListFragment { FirstFragmentSelectedListener mCallback; private OptionFirstSettingAdapter settingAdapter; private Context mContext; private ListView listView; private WifiP2pManager.Channel mChannel = null; private WifiP2pManager mWifiP2pManager; private WifiManager mWifiManager; public interface FirstFragmentSelectedListener { public void onFirstSelectedData(int dataPositon); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext = getActivity(); settingAdapter = new OptionFirstSettingAdapter(mContext, OptionKarrays.getOptionArraysBySource(mContext)); setListAdapter(settingAdapter); } @Override public void onStart() { super.onStart(); listView = getListView(); if (getFragmentManager().findFragmentById(R.id.first_fragment) != null) { listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); } listView.setDivider(null); listView.setDividerHeight(5); listView.setSelector(android.R.color.transparent); listView.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { settingAdapter.setSelectViewPosition(position); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); listView.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View view, int keyCode, KeyEvent keyEvent) { // TODO Auto-generated method stub if (OptionKarrays.isFromLauncher) { if (!isWifiOpen() && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {//wifi has not open if (listView.getSelectedItemPosition() == 1 && keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {//??? listView.setSelection(3); return true; } else if (listView.getSelectedItemPosition() == 3 && keyCode == KeyEvent.KEYCODE_DPAD_UP) { listView.setSelection(1); return true; } } } return false; } }); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mCallback = (FirstFragmentSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener"); } } @Override public void onListItemClick(ListView listView, View v, int position, long id) { settingAdapter.setSelectViewPosition(position); if (OptionKarrays.isFromLauncher) { if (position == 2 || position == 3) { mCallback.onFirstSelectedData(position); } else { handleAction(position); } } else { if (position != 2) { mCallback.onFirstSelectedData(position); } else { handleAction(position); } } } private void handleAction(int position) { if (OptionKarrays.isFromLauncher) { if (position == OptionKarrays.CONNECT_NETWORK) { Intent intent = new Intent(mContext, NetSettingActivity.class); //mengwt 20160120 add for set net not come back launcher start if (getActivity().getIntent().getBooleanExtra("fromLauncher", false)) { intent.putExtra("fromLauncher", true); } //mengwt 20160120 add for set net not come back launcher end mContext.startActivity(intent); getActivity().finish(); } else if (position == OptionKarrays.NETWORK_SETTINGS) { showNetWorkStatus(); } } else { if (position == OptionKarrays.CHANNEL_INFO) { showChannelInfo(); } } } private void showNetWorkStatus() { ((OptionMenuActivity) mContext).onRemoveMessage(); NetWorkStatusDialog statusDialog = new NetWorkStatusDialog(mContext, R.style.MyDialog); statusDialog.setOnDismissListener(new OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { ((OptionMenuActivity) mContext).onSendMessage(); } }); statusDialog.show(); } private void showChannelInfo() { ((OptionMenuActivity) mContext).onRemoveMessage(); ChannelInfoDialog channelInfoDialog = new ChannelInfoDialog(mContext, R.style.Dialog); channelInfoDialog.setOnDismissListener(new OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { ((OptionMenuActivity) mContext).onSendMessage(); } }); channelInfoDialog.show(); } //? private void clearScreenShares() { mWifiP2pManager = (WifiP2pManager) mContext.getSystemService(Context.WIFI_P2P_SERVICE); mChannel = mWifiP2pManager.initialize(mContext, mContext.getMainLooper(), null); mWifiP2pManager.removeGroup(mChannel, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { // TODO Auto-generated method stub showToast(mContext.getResources().getString(R.string.disconnect_success)); Log.i("PhoneShareReceiver", "???"); } @Override public void onFailure(int arg0) { // TODO Auto-generated method stub showToast(mContext.getResources().getString(R.string.disconnect_failed)); Log.i("PhoneShareReceiver", "??"); } }); } //wifi? private boolean isWifiOpen() { WifiManager wifiManager = getWifiManager(); // wifi is disabled if (wifiManager.isWifiEnabled()) { return true; } return false; } //wifi? private boolean isWifiConnected() { WifiManager wifiManager = getWifiManager(); // wifi is disabled if (!wifiManager.isWifiEnabled()) { return false; } // wifi have not connected WifiInfo info = wifiManager.getConnectionInfo(); if (info == null || info.getSSID() == null || info.getNetworkId() == WifiConfiguration.INVALID_NETWORK_ID) { return false; } return true; } //WifiManager private WifiManager getWifiManager() { if (mWifiManager == null) { mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); } return mWifiManager; } public boolean onKeyDown(int keyCode, KeyEvent event) { int selectPositon = getSelectedItemPosition(); switch (keyCode) { case KeyEvent.KEYCODE_DPAD_LEFT: break; case KeyEvent.KEYCODE_DPAD_RIGHT: if (selectPositon == -1) break; if (OptionKarrays.isFromLauncher) { if (selectPositon == 2 || selectPositon == 3) { mCallback.onFirstSelectedData(selectPositon); } } else { if (getSelectedItemPosition() != 2) mCallback.onFirstSelectedData(selectPositon); } break; } return false; } protected void showToast(String context) { Toast.makeText(mContext, context, Toast.LENGTH_SHORT).show(); ; } }