Example usage for android.os Bundle getChar

List of usage examples for android.os Bundle getChar

Introduction

In this page you can find the example usage for android.os Bundle getChar.

Prototype

@Override
public char getChar(String key, char defaultValue) 

Source Link

Document

Returns the value associated with the given key, or defaultValue if no mapping of the desired type exists for the given key.

Usage

From source file:Main.java

public static char interceptCharParam(Bundle savedInstanceState, Intent intent, String paramName) {
    char ret = 0;

    if (savedInstanceState != null) {
        ret = savedInstanceState.getChar(paramName, (char) 0);
    } else {//from   w ww  .  j  a v a  2s  .co  m
        if (intent != null) {
            Bundle incomming = intent.getExtras();
            if (incomming != null) {
                ret = incomming.getChar(paramName, (char) 0);
            }
        }
    }

    return ret;
}

From source file:com.shanet.relayremote.Background.java

protected ArrayList<BasicNameValuePair> doInBackground(Bundle... params) {
    // There should only be 1 bundle
    if (params.length != 1) {
        if (!isWidget) {
            ((Activity) context).runOnUiThread(new Runnable() {
                public void run() {
                    DialogUtils.displayErrorDialog(context, R.string.malformedDataErrorTitle,
                            R.string.malformedDataError);
                }//www .  j  a  v  a 2  s.c om
            });
        }
        return new ArrayList<BasicNameValuePair>();
    }

    // Get the UI info from the bundle
    Bundle info = params[0];
    op = info.getChar("op", Constants.OP_GET);
    char cmd = info.getChar("cmd", Constants.CMD_OFF);
    pin = info.getInt("pin", Constants.DEFAULT_PIN);
    String host = info.getString("server", "");
    int port = info.getInt("port", Constants.DEFAULT_PORT);
    appWidgetId = info.getInt("appWidgetId", -1);

    // Create the server
    Server server;
    String reply;
    ArrayList<BasicNameValuePair> states = new ArrayList<BasicNameValuePair>();

    try {
        server = new Server(host, port);
    } catch (SocketException e) {
        if (!isWidget) {
            ((Activity) context).runOnUiThread(new Runnable() {
                public void run() {
                    dialog.dismiss();
                    DialogUtils.displayErrorDialog(context, R.string.serverCommErrorTitle,
                            R.string.serverCommError);
                }
            });
        }
        return createErrorStatesArray(host, pin, cmd);
    }

    try {
        // Connect to the server if not already connected
        if (!server.isConnected()) {
            server.connect();

            // Ensure we're connected now
            if (!server.isConnected()) {
                if (!isWidget) {
                    ((Activity) context).runOnUiThread(new Runnable() {
                        public void run() {
                            DialogUtils.displayErrorDialog(context, R.string.serverConnectErrorTitle,
                                    R.string.serverConnectError);
                        }
                    });
                }
                throw new Exception();
            }
        }

        // Format and send the data to the server
        if (op == Constants.OP_GET) {
            // GET operations are just the letter "g"
            server.send(Constants.OP_GET + "\n");
        } else {
            // SET operations are of the form "s-[pin]-[state]"
            // Pin is the pin the relay is connected to
            // State is 0 for off, 1 for on, or t for toggle (not used in this app)
            server.send(Constants.OP_SET + "-" + pin + "-" + cmd + "\n");
        }

        // Get the reply from the server
        reply = server.receive();

        if (reply.equals("ERR")) {
            if (!isWidget) {
                ((Activity) context).runOnUiThread(new Runnable() {
                    public void run() {
                        DialogUtils.displayErrorDialog(context, R.string.serverErrorTitle,
                                R.string.serverError);
                    }
                });
            }
            throw new Exception();
            // Create the states array
        } else {
            // The first entry in the states list should be the server the states belong to
            states.add(new BasicNameValuePair("server", host));

            // If a get operation, format the reply
            if (op == Constants.OP_GET) {
                for (int i = 0; i < reply.length(); i += 4) {
                    states.add(new BasicNameValuePair(String.valueOf(reply.charAt(i)), String
                            .valueOf((reply.charAt(i + 2) == '1') ? Constants.CMD_ON : Constants.CMD_OFF)));
                }
                // Else, it's a set command so just add the pin we just handled
            } else {
                states.add(new BasicNameValuePair(String.valueOf(pin), String.valueOf(cmd)));
            }
        }

    } catch (UnknownHostException uhe) {
        states = createErrorStatesArray(host, pin, cmd);
        if (!isWidget) {
            ((Activity) context).runOnUiThread(new Runnable() {
                public void run() {
                    dialog.dismiss();
                    DialogUtils.displayErrorDialog(context, R.string.unknownHostErrorTitle,
                            R.string.unknownHostError);
                }
            });
        }
    } catch (final IOException ioe) {
        states = createErrorStatesArray(host, pin, cmd);
        if (!isWidget) {
            ((Activity) context).runOnUiThread(new Runnable() {
                public void run() {
                    dialog.dismiss();
                    DialogUtils.displayErrorDialog(context, context.getString(R.string.serverCommErrorTitle),
                            ioe.getMessage());
                }
            });
        }
    } catch (NullPointerException npe) {
        states = createErrorStatesArray(host, pin, cmd);
        if (!isWidget) {
            ((Activity) context).runOnUiThread(new Runnable() {
                public void run() {
                    dialog.dismiss();
                    DialogUtils.displayErrorDialog(context, R.string.serverCommErrorTitle,
                            R.string.serverCommError);
                }
            });
        }
    } catch (Exception e) {
        states = createErrorStatesArray(host, pin, cmd);
    } finally {
        // Shut down the server
        try {
            if (server != null && !server.isConnected())
                server.close();
        } catch (IOException ioe) {
        }
    }

    return states;
}