SetDeviceActivity.java 6.03 KB
package com.emmoco.example.blinker;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.emmoco.android.TargetConnection;

import java.util.ArrayList;

/**
 * This activity manages a screen where the user can choose from a list of target devices.  These devices include
 * the Bluetooth paired devices and the Emmoco mock devices used for testing.
 *
 * This activity maintains a simple listview.  There is an option to jump to the Settings->Bluetooth-Scan activity
 * When this activity returns, it refreshes the paired bluetooth device list in case it has changed
 *
 * This uses a custom Listview Adapter to show a checkmark next to the currently saved device
 *
 * The devices is stored using standard Android SharedPreferences
 *
 */
public class SetDeviceActivity extends Activity{
    final static private String TAG="SetDeviceActivity";

    private String              mCurrentDevice;
    private ListView            mDeviceList;
    private DeviceArrayAdapter  mAdapter;

    final public static String  PREF_DEVICE_NAME  = "DEVICE";
    final public static String  MOCK_DEVICE_LABEL = "Mock Blinker Device";


    /**
     * Internal class that adapts our array of device strings to a listview.  Current choice is highlighted with
     * a checkmark
     */
    private class DeviceArrayAdapter extends BaseAdapter {

        ArrayList<String>   mList;
        Activity            mContext;

        DeviceArrayAdapter(Activity context, ArrayList<String> arrayList)   {
            mList    = arrayList;
            mContext = context;
        }

        public int getCount() {
            return mList.size();
        }

        public Object getItem(int i) {
            return mList.get(i);
        }

        public long getItemId(int i) {
            return i;
        }

        /**
         * Returns a new view constructed from a simple layout.  The layout has a textview for the name of the device
         * and a checkmark graphic that is visible or not.
         *
         * @param i
         * @param view
         * @param viewGroup
         * @return
         */
        public View getView(int i, View view, ViewGroup viewGroup) {
            LayoutInflater inflator = mContext.getLayoutInflater();
            view = inflator.inflate(R.layout.list_item, null);

            String name  = (String)getItem(i);
            String label = name;

            // Translate the URI for the device into a pretty label for display in the listview

            // If Mock device, use mock label
            if (name.contentEquals(TargetConnection.MOCK_BLINKER_PRE)){
                label = MOCK_DEVICE_LABEL;
            }
            if (name.startsWith(TargetConnection.BT_PRE)) {
                // If Bluetooth, remove prefix
                label = name.substring(TargetConnection.BT_PRE.length());
            }


            // Set the label in the view
            ((TextView)view.findViewById(R.id.item_label)).setText(label);

            // If the current one, show the checkbox
            if (mList.get(i).contentEquals(mCurrentDevice)){
                view.findViewById(R.id.checkmark).setVisibility(View.VISIBLE);
            }else{
                view.findViewById(R.id.checkmark).setVisibility(View.INVISIBLE);
            }
            return view;
        }

        // Replace the list with a new one and trigger a redraw on the screen
        public void updateList(ArrayList<String> list){
            mList = list;
            notifyDataSetInvalidated();
        }
    }

    /**
     * Standard Android method called at start of activity.
     *
     * Save the
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.set_device_activity);


        mDeviceList = (ListView)findViewById(R.id.device_list_view);

        mAdapter = new DeviceArrayAdapter(this,TargetConnection.deviceList(true,false));
        mDeviceList.setAdapter(mAdapter);

        // get the device to connect to from the users preferences
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        mCurrentDevice          = prefs.getString(PREF_DEVICE_NAME, TargetConnection.MOCK_BLINKER_PRE);


        // Register a handler when one of the list items is clicked.  This handler will set the
        // preference to use the new device, and finish this activity so we go back to the main activity
        mDeviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                String newDeviceName = (String)mAdapter.getItem(i);

                // Save changed name to preferences
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SetDeviceActivity.this);
                SharedPreferences.Editor edit = prefs.edit();
                edit.putString(PREF_DEVICE_NAME, newDeviceName);
                edit.commit();

                // Set the result so the calling activity know we have changed the device
                setResult(RESULT_OK);
                finish();
            }
        } );

    }

    @Override
    protected void onResume() {
        // If we are resuming after going to Settings-->Bluetooth, the device list may have changed so update
        mAdapter.updateList(TargetConnection.deviceList(true,false));

        super.onResume();
    }



    /**
     * Use an intent to call the Android bluetooth settings for scanning bluetooth devices
     * @param v
     */
    public void onScanDeviceClick(View v) {
        Intent i =  new Intent();
        i.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
        startActivity(i);
    }

}