MainView.java

/**
 * Copyright (c) 2013 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.phonesettings;

import com.nokia.mid.network.NetworkState;
import com.nokia.mid.network.NetworkStateListener;
import com.nokia.mid.network.SIMState;
import com.nokia.mid.network.SIMStateListener;
import com.nokia.mid.network.WLANState;
import com.nokia.mid.network.WLANStateListener;

import com.nokia.mid.setting.Setting;
import com.nokia.mid.setting.SettingListener;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

/**
 * The main view of the application. Displays the list of various phone
 * settings.
 */
public class MainView
    extends List
    implements CommandListener,
               SettingListener,
               NetworkStateListener,
               SIMStateListener,
               WLANStateListener
{
    // Constants
    public static final int PHONE_SETTING_INDEX = 0;
    public static final int NETWORK_STATE_INDEX = 1;
    public static final int SIM_STATE_INDEX = 2;
    public static final int WLAN_STATE_INDEX = 3;
    public static final String[] PHONE_SETTING_STRING_LIST =
        { "Phone Settings ", "Network State", "SIM State", "WLAN State" };

    // Members
    private SettingsView settingsView = null;
    private Command exitCommand = null;
    private Command backCommand = null;
    private Display display = null;
    private int index = 0;

    /**
     * Constructor.
     */
    public MainView(Display display) {
        super("Phone Settings", List.IMPLICIT, PHONE_SETTING_STRING_LIST, null);
        this.display = display;
        
        settingsView = new SettingsView(PHONE_SETTING_STRING_LIST[PHONE_SETTING_INDEX]);
        backCommand = new Command("Back", Command.BACK, 1);
        settingsView.addCommand(backCommand);
        settingsView.setCommandListener(this);
        
        exitCommand = new Command("Exit", Command.EXIT, 1);
        addCommand(exitCommand);
        setCommandListener(this);
        
        Setting.subscribeListener(this);
        NetworkState.subscribeListener(this);
        SIMState.subscribeListener(this);
        WLANState.subscribeListener(this);
    }

    /**
     * @see javax.microedition.lcdui.CommandListener#commandAction(Command, Displayable)
     */
    public void commandAction(Command command, Displayable displayable) {
        if (command == List.SELECT_COMMAND) {
            settingsView.update(getSelectedIndex());
            display.setCurrent(settingsView);
        }
        else if (command == backCommand) {
            // This command was received by the settings view. Hide the
            // settings view and show this view.
            display.setCurrent(this);
        }
        else if (command == exitCommand) {
            Main.quit();
        }
    }


    // Call backs from various setting listeners

     /**
     * @see com.nokia.mid.setting.SettingListener#settingChanged(int, int) 
     */
    public void settingChanged(int setting, int value) {
        index = PHONE_SETTING_INDEX;
        settingsView.update(index);
        display.setCurrent(settingsView);
    }

    /**
     * @see com.nokia.mid.network.NetworkStateListener#networkStateChanged(int, int) 
     */
    public void networkStateChanged(int simIndex, int state) {
        index = NETWORK_STATE_INDEX;
        settingsView.update(index);
        display.setCurrent(settingsView);
    }

    /**
     * @see com.nokia.mid.network.SIMStateListener#SIMStateChanged(int, int) 
     */
    public void SIMStateChanged(int simIndex, int state) {
        index = SIM_STATE_INDEX;
        settingsView.update(index);
        display.setCurrent(settingsView);
    }

    /**
     * @see com.nokia.mid.network.WLANStateListener#WLANStateChanged(int) 
     */
    public void WLANStateChanged(int state) {
        index = WLAN_STATE_INDEX;
        settingsView.update(index);
        display.setCurrent(settingsView);
    }
}