com.mstar.tv.tvplayer.philips.setting.SettingFirstFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.mstar.tv.tvplayer.philips.setting.SettingFirstFragment.java

Source

package com.mstar.tv.tvplayer.philips.setting;
/*
 * 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.
 */

import com.mstar.android.tv.TvCommonManager;
import com.mstar.tv.tvplayer.kangguan.tvapi.K_Constants;
import com.mstar.tv.tvplayer.kangguan.tvapi.K_TvCommonManager;
import com.mstar.tv.tvplayer.ui.R;

import android.app.Activity;
import android.support.v4.app.ListFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.database.Cursor;
import android.net.Uri;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.Toast;

public class SettingFirstFragment extends ListFragment {
    FirstFragmentSelectedListener mCallback;
    private SettingFirstViewSettingAdapter settingAdapter;
    private Context mContext;
    private ListView listView;
    private String[] dataArray;
    private TvCommonManager mTvCommonManager = null;

    public interface FirstFragmentSelectedListener {
        public void onFirstSelectedData(String[] dataArray, int dataPositon);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        mTvCommonManager = TvCommonManager.getInstance();
        settingAdapter = new SettingFirstViewSettingAdapter(mContext,
                SettingKarrays.getinstance(mContext).getFirstArrays(0));
        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) {
                if (parent.isFocused())
                    settingAdapter.setSelectViewPosition(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        listView.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    settingAdapter.setSelectViewPosition(getSelectedItemPosition());
                } else {
                    settingAdapter.setSelectViewPosition(-1);
                }
            }
        });
        listView.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                if (TvSettingMenuActivity.firstSelectPositon == SettingKarrays.SETTING_PHOTO) {
                    if (K_TvCommonManager.getInstance().K_getCurrentTvInputSource() == K_Constants.INPUT_SOURCE_VGA
                            && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {

                        if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                            if (listView.getSelectedItemPosition() == 2) {
                                listView.setSelection(5);
                                return true;
                            }
                        } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                            if (listView.getSelectedItemPosition() == 5) {
                                listView.setSelection(2);
                                return true;
                            } else if (listView.getSelectedItemPosition() == 1) {
                                //listView.setSelection(1);
                                return true;
                            }
                        }
                    }
                } else if (TvSettingMenuActivity.firstSelectPositon == SettingKarrays.SETTING_TV) {
                    if (!(mTvCommonManager.getCurrentTvInputSource() == TvCommonManager.INPUT_SOURCE_HDMI
                            || mTvCommonManager.getCurrentTvInputSource() == TvCommonManager.INPUT_SOURCE_HDMI2
                            || mTvCommonManager.getCurrentTvInputSource() == TvCommonManager.INPUT_SOURCE_HDMI3)) {//EDID??
                        if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                            if (listView.getSelectedItemPosition() == 4) {
                                listView.setSelection(6);
                                return true;
                            }
                        } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP
                                && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                            if (listView.getSelectedItemPosition() == 6) {
                                listView.setSelection(4);
                                return true;
                            }
                        }
                    }
                }
                return false;
            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            getListView().setFocusable(false);
        } else {
            getListView().setFocusable(true);
            getListView().requestFocus();
        }
    }

    @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 (TvSettingMenuActivity.firstSelectPositon == SettingKarrays.SETTING_ECO) {
            if (position == 2) {
                handleAction(position);
            } else {
                mCallback.onFirstSelectedData(dataArray, position);
            }
        } else if (TvSettingMenuActivity.firstSelectPositon == SettingKarrays.SETTING_TV) {
            if (position == 0 || position == 6) {//yzh:5-->6
                handleAction(position);
            } else {
                mCallback.onFirstSelectedData(dataArray, position);
            }
        } else if (TvSettingMenuActivity.firstSelectPositon == SettingKarrays.SETTING_CHANNEL) {
            if (position == 0 || position == 1) {
                handleAction(position);
            } else {
                mCallback.onFirstSelectedData(dataArray, position);
            }
        } else {
            mCallback.onFirstSelectedData(dataArray, position);
        }

    }

    private void handleAction(int position) {
        if (TvSettingMenuActivity.firstSelectPositon == SettingKarrays.SETTING_ECO) {
            showSeperateHearDialog();
        } else if (TvSettingMenuActivity.firstSelectPositon == SettingKarrays.SETTING_TV) {
            if (position == 0) {
                showResetMenuDialog();
            } else if (position == 6) {
                showReinstallTvDialog();
            }
        } else if (TvSettingMenuActivity.firstSelectPositon == SettingKarrays.SETTING_CHANNEL) {
            if (position == 0) {
                showChannelSettingActivity();
            } else if (position == 1) {
                showDTVManualSearchChannelDialog();
            } else if (position == 6) {
                showReinstallTvDialog();
            }
        }
    }

    private void showDTVManualSearchChannelDialog() {
        DTVManualSearchChannelDialog dialog = new DTVManualSearchChannelDialog(mContext, R.style.Dialog);
        dialog.setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface dialog) {
                ((TvSettingMenuActivity) mContext).onSendMessage();
            }
        });
        ((TvSettingMenuActivity) mContext).onRemoveMessage();
        dialog.show();

    }

    private void showChannelSettingActivity() {
        Intent mIntent = new Intent(mContext, ChannelSettingActivity.class);
        startActivity(mIntent);
        getActivity().finish();
    }

    private void showResetMenuDialog() {
        if (isHotelSpecialMode_On()) {
            Toast.makeText(mContext, mContext.getResources().getString(R.string.str_hotel_specialmode_on),
                    Toast.LENGTH_LONG).show();
        } else {
            MenuResetDialog dialog = new MenuResetDialog(mContext, R.style.Dialog);
            dialog.show();
        }
    }

    private void showReinstallTvDialog() {
        if (isHotelSpecialMode_On()) {
            Toast.makeText(mContext, mContext.getResources().getString(R.string.str_hotel_specialmode_on),
                    Toast.LENGTH_LONG).show();
        } else {
            ReInstallTvDialog dialog = new ReInstallTvDialog(mContext, R.style.Dialog);
            dialog.show();
        }
    }

    private void showSeperateHearDialog() {
        SeperateHearDialog dialog = new SeperateHearDialog(mContext, R.style.Dialog);
        dialog.setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface dialog) {
                ((TvSettingMenuActivity) mContext).onSendMessage();
            }
        });
        ((TvSettingMenuActivity) mContext).onRemoveMessage();
        dialog.show();
    }

    public void updateView(int dataPostiton, boolean state) {
        dataArray = SettingKarrays.getinstance(mContext).getFirstArrays(dataPostiton);
        settingAdapter = new SettingFirstViewSettingAdapter(mContext, dataArray);
        setListAdapter(settingAdapter);

        if (state) {
            if (TvSettingMenuActivity.firstSelectPositon == SettingKarrays.SETTING_PHOTO && K_TvCommonManager
                    .getInstance().K_getCurrentTvInputSource() == K_Constants.INPUT_SOURCE_VGA) {
                getListView().setSelection(1);
                settingAdapter.setSelectViewPosition(1);
                getListView().setFocusable(true);
                getListView().requestFocus();
                getListView().setEnabled(true);
            } else {
                settingAdapter.setSelectViewPosition(0);
                getListView().setFocusable(true);
                getListView().requestFocus();
                getListView().setEnabled(true);
            }
        } else {
            settingAdapter.setSelectViewPosition(-1);
            getListView().setFocusable(false);
            getListView().setEnabled(false);
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        int position = getSelectedItemPosition();
        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (TvSettingMenuActivity.firstSelectPositon == SettingKarrays.SETTING_ECO) {
                if (getSelectedItemPosition() != 2) {
                    mCallback.onFirstSelectedData(dataArray, getSelectedItemPosition());
                }
            } else if (TvSettingMenuActivity.firstSelectPositon == SettingKarrays.SETTING_TV) {
                if (position != 0 && position != 6) {//yzh:5-->6
                    mCallback.onFirstSelectedData(dataArray, getSelectedItemPosition());
                }
            } else if (TvSettingMenuActivity.firstSelectPositon == SettingKarrays.SETTING_CHANNEL) {
                if (position != 0 && position != 1) {
                    mCallback.onFirstSelectedData(dataArray, getSelectedItemPosition());
                }
            } else {
                mCallback.onFirstSelectedData(dataArray, getSelectedItemPosition());
            }
            break;
        }
        return false;
    }

    private boolean isHotelSpecialMode_On() {
        int flag = 0;
        Cursor cursor = mContext.getContentResolver()
                .query(Uri.parse("content://mstar.tv.usersetting/systemsetting"), null, null, null, null);
        if (cursor.moveToFirst()) {
            flag = cursor.getInt(cursor.getColumnIndex("Philips_SpecialMode"));
        }
        cursor.close();
        return flag == 0 ? false : true;
    }
}