New API showing the various new service binding flags.

Change-Id: I2c19388c1c8b8386d77183c1e1f3aa9eef85cd46
diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml
index e34cd90..b70ed4d 100644
--- a/samples/ApiDemos/AndroidManifest.xml
+++ b/samples/ApiDemos/AndroidManifest.xml
@@ -489,6 +489,14 @@
             </intent-filter>
         </activity>
 
+        <activity android:name=".app.RemoteService$BindingOptions"
+                android:label="@string/activity_remote_service_binding_options">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.SAMPLE_CODE" />
+            </intent-filter>
+        </activity>
+
         <service android:name=".app.ServiceStartArguments" />
 
         <activity android:name=".app.ServiceStartArguments$Controller"
diff --git a/samples/ApiDemos/res/layout/remote_binding_options.xml b/samples/ApiDemos/res/layout/remote_binding_options.xml
new file mode 100644
index 0000000..aec33f8
--- /dev/null
+++ b/samples/ApiDemos/res/layout/remote_binding_options.xml
@@ -0,0 +1,82 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2007 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.
+-->
+
+<!-- Demonstrates starting and stopping a local service.
+     See corresponding Java code com.android.sdk.app.LocalSerice.java. -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
+    android:gravity="center_horizontal"
+    android:layout_width="match_parent" android:layout_height="match_parent">
+
+    <TextView
+        android:layout_width="match_parent" android:layout_height="wrap_content"
+        android:layout_weight="0"
+        android:paddingBottom="4dip"
+        android:text="Demonstrates various options that can be used when binding to a service to modify its process management."/>
+
+    <TextView android:id="@+id/callback"
+        android:layout_width="match_parent" android:layout_height="wrap_content"
+        android:layout_weight="0"
+        android:gravity="center_horizontal" android:paddingBottom="4dip"/>
+
+    <ScrollView android:layout_width="match_parent" android:layout_height="match_parent">
+        <LinearLayout android:orientation="vertical"
+            android:gravity="center_horizontal"
+            android:layout_width="match_parent" android:layout_height="wrap_content">
+            <Button android:id="@+id/bind_normal"
+                android:layout_width="wrap_content" android:layout_height="wrap_content"
+                android:text="Normal">
+                <requestFocus />
+            </Button>
+
+            <Button android:id="@+id/bind_not_foreground"
+                android:layout_width="wrap_content" android:layout_height="wrap_content"
+                android:text="Not Foreground">
+            </Button>
+
+            <Button android:id="@+id/bind_above_client"
+                android:layout_width="wrap_content" android:layout_height="wrap_content"
+                android:text="Above Client">
+            </Button>
+
+            <Button android:id="@+id/bind_allow_oom"
+                android:layout_width="wrap_content" android:layout_height="wrap_content"
+                android:text="Allow OOM Management">
+            </Button>
+
+            <Button android:id="@+id/bind_waive_priority"
+                android:layout_width="wrap_content" android:layout_height="wrap_content"
+                android:text="Waive Priority">
+            </Button>
+
+            <Button android:id="@+id/bind_important"
+                android:layout_width="wrap_content" android:layout_height="wrap_content"
+                android:text="Important">
+            </Button>
+
+            <Button android:id="@+id/bind_with_activity"
+                android:layout_width="wrap_content" android:layout_height="wrap_content"
+                android:text="Adjust With Activity">
+            </Button>
+
+            <Button android:id="@+id/unbind"
+                android:layout_width="wrap_content" android:layout_height="wrap_content"
+                android:text="@string/unbind_service">
+            </Button>
+        </LinearLayout>
+    </ScrollView>
+
+</LinearLayout>
diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml
index 428d2cc..1aaff9a 100644
--- a/samples/ApiDemos/res/values/strings.xml
+++ b/samples/ApiDemos/res/values/strings.xml
@@ -208,8 +208,11 @@
     <string name="kill_process">Kill Process</string>
     <string name="remote_service_connected">Connected to remote service</string>
     <string name="remote_service_disconnected">Disconnected from remote service</string>
+    <string name="remote_service_unbind_disconn">Unbinding due to disconnect</string>
     <string name="remote_call_failed">Failure calling remote service</string>
 
+    <string name="activity_remote_service_binding_options">App/Service/Remote Service Binding Options</string>
+
     <string name="service_start_arguments_label">Sample Service Start Arguments
     </string>
 
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/RemoteService.java b/samples/ApiDemos/src/com/example/android/apis/app/RemoteService.java
index aade659..76458f4 100644
--- a/samples/ApiDemos/src/com/example/android/apis/app/RemoteService.java
+++ b/samples/ApiDemos/src/com/example/android/apis/app/RemoteService.java
@@ -461,4 +461,192 @@
         };
     }
 // END_INCLUDE(calling_a_service)
+
+    // ----------------------------------------------------------------------
+
+    /**
+     * Examples of behavior of different bind flags.</p>
+     */
+ // BEGIN_INCLUDE(calling_a_service)
+    public static class BindingOptions extends Activity {
+        ServiceConnection mCurConnection;
+        TextView mCallbackText;
+
+        class MyServiceConnection implements ServiceConnection {
+            final boolean mUnbindOnDisconnect;
+
+            public MyServiceConnection() {
+                mUnbindOnDisconnect = false;
+            }
+
+            public MyServiceConnection(boolean unbindOnDisconnect) {
+                mUnbindOnDisconnect = unbindOnDisconnect;
+            }
+
+            public void onServiceConnected(ComponentName className,
+                    IBinder service) {
+                if (mCurConnection != this) {
+                    return;
+                }
+                mCallbackText.setText("Attached.");
+                Toast.makeText(BindingOptions.this, R.string.remote_service_connected,
+                        Toast.LENGTH_SHORT).show();
+            }
+
+            public void onServiceDisconnected(ComponentName className) {
+                if (mCurConnection != this) {
+                    return;
+                }
+                mCallbackText.setText("Disconnected.");
+                Toast.makeText(BindingOptions.this, R.string.remote_service_disconnected,
+                        Toast.LENGTH_SHORT).show();
+                if (mUnbindOnDisconnect) {
+                    unbindService(this);
+                    mCurConnection = null;
+                    Toast.makeText(BindingOptions.this, R.string.remote_service_unbind_disconn,
+                            Toast.LENGTH_SHORT).show();
+                }
+            }
+        }
+
+        /**
+         * Standard initialization of this activity.  Set up the UI, then wait
+         * for the user to poke it before doing anything.
+         */
+        @Override
+        protected void onCreate(Bundle savedInstanceState) {
+            super.onCreate(savedInstanceState);
+
+            setContentView(R.layout.remote_binding_options);
+
+            // Watch for button clicks.
+            Button button = (Button)findViewById(R.id.bind_normal);
+            button.setOnClickListener(mBindNormalListener);
+            button = (Button)findViewById(R.id.bind_not_foreground);
+            button.setOnClickListener(mBindNotForegroundListener);
+            button = (Button)findViewById(R.id.bind_above_client);
+            button.setOnClickListener(mBindAboveClientListener);
+            button = (Button)findViewById(R.id.bind_allow_oom);
+            button.setOnClickListener(mBindAllowOomListener);
+            button = (Button)findViewById(R.id.bind_waive_priority);
+            button.setOnClickListener(mBindWaivePriorityListener);
+            button = (Button)findViewById(R.id.bind_important);
+            button.setOnClickListener(mBindImportantListener);
+            button = (Button)findViewById(R.id.bind_with_activity);
+            button.setOnClickListener(mBindWithActivityListener);
+            button = (Button)findViewById(R.id.unbind);
+            button.setOnClickListener(mUnbindListener);
+
+            mCallbackText = (TextView)findViewById(R.id.callback);
+            mCallbackText.setText("Not attached.");
+        }
+
+        private OnClickListener mBindNormalListener = new OnClickListener() {
+            public void onClick(View v) {
+                if (mCurConnection != null) {
+                    unbindService(mCurConnection);
+                    mCurConnection = null;
+                }
+                ServiceConnection conn = new MyServiceConnection();
+                if (bindService(new Intent(IRemoteService.class.getName()),
+                        conn, Context.BIND_AUTO_CREATE)) {
+                    mCurConnection = conn;
+                }
+            }
+        };
+
+        private OnClickListener mBindNotForegroundListener = new OnClickListener() {
+            public void onClick(View v) {
+                if (mCurConnection != null) {
+                    unbindService(mCurConnection);
+                    mCurConnection = null;
+                }
+                ServiceConnection conn = new MyServiceConnection();
+                if (bindService(new Intent(IRemoteService.class.getName()),
+                        conn, Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND)) {
+                    mCurConnection = conn;
+                }
+            }
+        };
+
+        private OnClickListener mBindAboveClientListener = new OnClickListener() {
+            public void onClick(View v) {
+                if (mCurConnection != null) {
+                    unbindService(mCurConnection);
+                    mCurConnection = null;
+                }
+                ServiceConnection conn = new MyServiceConnection();
+                if (bindService(new Intent(IRemoteService.class.getName()),
+                        conn, Context.BIND_AUTO_CREATE | Context.BIND_ABOVE_CLIENT)) {
+                    mCurConnection = conn;
+                }
+            }
+        };
+
+        private OnClickListener mBindAllowOomListener = new OnClickListener() {
+            public void onClick(View v) {
+                if (mCurConnection != null) {
+                    unbindService(mCurConnection);
+                    mCurConnection = null;
+                }
+                ServiceConnection conn = new MyServiceConnection();
+                if (bindService(new Intent(IRemoteService.class.getName()),
+                        conn, Context.BIND_AUTO_CREATE | Context.BIND_ALLOW_OOM_MANAGEMENT)) {
+                    mCurConnection = conn;
+                }
+            }
+        };
+
+        private OnClickListener mBindWaivePriorityListener = new OnClickListener() {
+            public void onClick(View v) {
+                if (mCurConnection != null) {
+                    unbindService(mCurConnection);
+                    mCurConnection = null;
+                }
+                ServiceConnection conn = new MyServiceConnection(true);
+                if (bindService(new Intent(IRemoteService.class.getName()),
+                        conn, Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY)) {
+                    mCurConnection = conn;
+                }
+            }
+        };
+
+        private OnClickListener mBindImportantListener = new OnClickListener() {
+            public void onClick(View v) {
+                if (mCurConnection != null) {
+                    unbindService(mCurConnection);
+                    mCurConnection = null;
+                }
+                ServiceConnection conn = new MyServiceConnection();
+                if (bindService(new Intent(IRemoteService.class.getName()),
+                        conn, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT)) {
+                    mCurConnection = conn;
+                }
+            }
+        };
+
+        private OnClickListener mBindWithActivityListener = new OnClickListener() {
+            public void onClick(View v) {
+                if (mCurConnection != null) {
+                    unbindService(mCurConnection);
+                    mCurConnection = null;
+                }
+                ServiceConnection conn = new MyServiceConnection();
+                if (bindService(new Intent(IRemoteService.class.getName()),
+                        conn, Context.BIND_AUTO_CREATE | Context.BIND_ADJUST_WITH_ACTIVITY
+                        | Context.BIND_WAIVE_PRIORITY)) {
+                    mCurConnection = conn;
+                }
+            }
+        };
+
+        private OnClickListener mUnbindListener = new OnClickListener() {
+            public void onClick(View v) {
+                if (mCurConnection != null) {
+                    unbindService(mCurConnection);
+                    mCurConnection = null;
+                }
+            }
+        };
+    }
 }