upload android base code part7

This commit is contained in:
August 2018-08-08 18:09:17 +08:00
parent 4e516ec6ed
commit 841ae54672
25229 changed files with 1709508 additions and 0 deletions

View file

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2015 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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.directshare"
android:versionCode="1"
android:versionName="1.0">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/DirectShareTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SendMessageActivity"
android:label="@string/app_name"
android:theme="@style/DirectShareDialogTheme">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<meta-data
android:name="android.service.chooser.chooser_target_service"
android:value=".SampleChooserTargetService" />
</activity>
<activity
android:name=".SelectContactActivity"
android:label="@string/app_name"
android:theme="@style/DirectShareDialogTheme" />
<service
android:name=".SampleChooserTargetService"
android:label="@string/app_name"
android:permission="android.permission.BIND_CHOOSER_TARGET_SERVICE">
<intent-filter>
<action android:name="android.service.chooser.ChooserTargetService" />
</intent-filter>
</service>
</application>
</manifest>

View file

@ -0,0 +1,11 @@
page.tags="DirectShare"
sample.group=Content
@jd:body
<p>
This sample demonstrates how to provide the Direct Share feature. The app shows some options
directly in the list of share intent candidates.
</p>

Binary file not shown.

After

Width:  |  Height:  |  Size: 196 B

View file

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2015 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.
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/contact_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawablePadding="16dp"
android:gravity="center_vertical"
android:paddingBottom="4dp"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingTop="4dp"
android:textAppearance="@android:style/TextAppearance.Material.Body1"
tools:drawableStart="@mipmap/logo_avatar"
tools:text="Taro" />

View file

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?><!--
Copyright 2015 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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/colorPrimary"
android:elevation="4dp"
android:minHeight="?android:attr/actionBarSize"
android:popupTheme="@android:style/ThemeOverlay.Material.Light"
android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/explanation"
android:textAppearance="@android:style/TextAppearance.Material.Body1" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
<EditText
android:id="@+id/body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:hint="@string/text_to_share"
android:text="@string/hello" />
<Button
android:id="@+id/share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:text="@string/share" />
</LinearLayout>

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2015 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.
-->
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:clipToPadding="false"
android:divider="@null"
android:paddingBottom="4dp"
android:paddingTop="4dp" />

View file

@ -0,0 +1,84 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2015 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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/dialog_padding"
android:layout_marginStart="@dimen/dialog_padding"
android:layout_marginTop="@dimen/dialog_padding"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="@string/to"
android:textAppearance="@android:style/TextAppearance.Material.Caption" />
<TextView
android:id="@+id/contact_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_weight="1"
android:drawablePadding="16dp"
android:gravity="center_vertical"
android:textAppearance="@android:style/TextAppearance.Material.Body1"
tools:drawableStart="@mipmap/logo_avatar"
tools:text="Taro" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/dialog_padding"
android:layout_marginStart="@dimen/dialog_padding"
android:layout_marginTop="16dp"
android:gravity="center_vertical"
android:text="@string/body"
android:textAppearance="@android:style/TextAppearance.Material.Caption" />
<TextView
android:id="@+id/message_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/dialog_padding"
android:layout_marginStart="@dimen/dialog_padding"
android:gravity="top"
android:hint="@string/hint_body"
android:padding="8dp"
android:textAppearance="@android:style/TextAppearance.Material.Body1"
tools:text="Hello, world!" />
<Button
android:id="@+id/send"
style="@android:style/Widget.Material.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginBottom="@dimen/dialog_button_padding"
android:layout_marginEnd="@dimen/dialog_button_padding"
android:text="@string/send" />
</LinearLayout>

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

View file

@ -0,0 +1,24 @@
<!--
Copyright 2013 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.
-->
<resources>
<!-- Semantic definitions -->
<dimen name="horizontal_page_margin">@dimen/margin_huge</dimen>
<dimen name="vertical_page_margin">@dimen/margin_medium</dimen>
</resources>

View file

@ -0,0 +1,25 @@
<!--
Copyright 2013 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.
-->
<resources>
<style name="Widget.SampleMessage">
<item name="android:textAppearance">?android:textAppearanceLarge</item>
<item name="android:lineSpacingMultiplier">1.2</item>
<item name="android:shadowDy">-6.5</item>
</style>
</resources>

View file

@ -0,0 +1,22 @@
<!--
Copyright 2013 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.
-->
<resources>
<!-- Activity themes -->
<style name="Theme.Base" parent="android:Theme.Holo.Light" />
</resources>

View file

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2013 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.
-->
<resources>
</resources>

View file

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2013 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.
-->
<resources>
<!-- Activity themes -->
<style name="Theme.Base" parent="android:Theme.Material.Light">
</style>
</resources>

View file

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2013 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.
-->
<resources>
<string name="app_name">DirectShare</string>
<string name="intro_message">
<![CDATA[
This sample demonstrates how to provide the Direct Share feature. The app shows some options
directly in the list of share intent candidates.
]]>
</string>
</resources>

View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2015 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.
-->
<resources>
<color name="primary">#3F51B5</color>
<color name="primary_dark">#303F9F</color>
<color name="primary_light">#C5CAE9</color>
<color name="accent">#00BCD4</color>
<color name="primary_text">#212121</color>
<color name="secondary_text">#727272</color>
<color name="icons">#FFFFFF</color>
<color name="divider">#B6B6B6</color>
</resources>

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2015 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.
-->
<resources>
<dimen name="dialog_padding">24dp</dimen>
<dimen name="dialog_button_padding">8dp</dimen>
</resources>

View file

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2015 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.
-->
<resources>
<!-- MainActivity -->
<string name="explanation">
This app demonstrates how to implement Direct Share. Use some other app and share a text.
For your convenience, you can also use the input below to share the text.
</string>
<string name="hello">Hello!</string>
<string name="share">Share</string>
<string name="send_intent_title">Send a message via:</string>
<!-- SendMessageActivity -->
<string name="sending_message">Sending a message</string>
<string name="to">To:</string>
<string name="send">Send</string>
<string name="body">Body:</string>
<string name="hint_body">Edit your message.</string>
<string name="message_sent">Sent a message \"%1$s\" to %2$s.</string>
<string name="text_to_share">Text to share</string>
</resources>

View file

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2015 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.
-->
<resources>
<style name="DirectShareTheme" parent="android:Theme.Material.Light.NoActionBar">
<item name="android:colorPrimary">@color/primary</item>
<item name="android:colorPrimaryDark">@color/primary_dark</item>
<item name="android:colorAccent">@color/accent</item>
</style>
<style name="DirectShareDialogTheme" parent="android:Theme.Material.Light.Dialog">
<item name="android:colorPrimary">@color/primary</item>
<item name="android:colorPrimaryDark">@color/primary_dark</item>
<item name="android:colorAccent">@color/accent</item>
</style>
</resources>

View file

@ -0,0 +1,32 @@
<!--
Copyright 2013 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.
-->
<resources>
<!-- Define standard dimensions to comply with Holo-style grids and rhythm. -->
<dimen name="margin_tiny">4dp</dimen>
<dimen name="margin_small">8dp</dimen>
<dimen name="margin_medium">16dp</dimen>
<dimen name="margin_large">32dp</dimen>
<dimen name="margin_huge">64dp</dimen>
<!-- Semantic definitions -->
<dimen name="horizontal_page_margin">@dimen/margin_medium</dimen>
<dimen name="vertical_page_margin">@dimen/margin_medium</dimen>
</resources>

View file

@ -0,0 +1,42 @@
<!--
Copyright 2013 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.
-->
<resources>
<!-- Activity themes -->
<style name="Theme.Base" parent="android:Theme.Light" />
<style name="Theme.Sample" parent="Theme.Base" />
<style name="AppTheme" parent="Theme.Sample" />
<!-- Widget styling -->
<style name="Widget" />
<style name="Widget.SampleMessage">
<item name="android:textAppearance">?android:textAppearanceMedium</item>
<item name="android:lineSpacingMultiplier">1.1</item>
</style>
<style name="Widget.SampleMessageTile">
<item name="android:background">@drawable/tile</item>
<item name="android:shadowColor">#7F000000</item>
<item name="android:shadowDy">-3.5</item>
<item name="android:shadowRadius">2</item>
</style>
</resources>

View file

@ -0,0 +1,93 @@
/*
* Copyright (C) 2015 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.
*/
package com.example.android.directshare;
/**
* Provides the list of dummy contacts. This sample implements this as constants, but real-life apps
* should use a database and such.
*/
public class Contact {
/**
* The list of dummy contacts.
*/
public static final Contact[] CONTACTS = {
new Contact("Tereasa"),
new Contact("Chang"),
new Contact("Kory"),
new Contact("Clare"),
new Contact("Landon"),
new Contact("Kyle"),
new Contact("Deana"),
new Contact("Daria"),
new Contact("Melisa"),
new Contact("Sammie"),
};
/**
* The contact ID.
*/
public static final String ID = "contact_id";
/**
* Representative invalid contact ID.
*/
public static final int INVALID_ID = -1;
/**
* The name of this contact.
*/
private final String mName;
/**
* Instantiates a new {@link Contact}.
*
* @param name The name of the contact.
*/
public Contact(String name) {
mName = name;
}
/**
* Finds a {@link Contact} specified by a contact ID.
*
* @param id The contact ID. This needs to be a valid ID.
* @return A {@link Contact}
*/
public static Contact byId(int id) {
return CONTACTS[id];
}
/**
* Gets the name of this contact.
*
* @return The name of this contact.
*/
public String getName() {
return mName;
}
/**
* Gets the icon of this contact.
*
* @return The icon.
*/
public int getIcon() {
return R.mipmap.logo_avatar;
}
}

View file

@ -0,0 +1,37 @@
/*
* Copyright (C) 2015 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.
*/
package com.example.android.directshare;
import android.widget.TextView;
/**
* A simple utility to bind a {@link TextView} with a {@link Contact}.
*/
public class ContactViewBinder {
/**
* Binds the {@code textView} with the specified {@code contact}.
*
* @param contact The contact.
* @param textView The TextView.
*/
public static void bind(Contact contact, TextView textView) {
textView.setText(contact.getName());
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(contact.getIcon(), 0, 0, 0);
}
}

View file

@ -0,0 +1,64 @@
/*
* Copyright (C) 2015 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.
*/
package com.example.android.directshare;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toolbar;
/**
* Provides the landing screen of this sample. There is nothing particularly interesting here. All
* the codes related to the Direct Share feature are in {@link SampleChooserTargetService}.
*/
public class MainActivity extends Activity {
private EditText mEditBody;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setActionBar((Toolbar) findViewById(R.id.toolbar));
mEditBody = (EditText) findViewById(R.id.body);
findViewById(R.id.share).setOnClickListener(mOnClickListener);
}
private View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.share:
share();
break;
}
}
};
/**
* Emits a sample share {@link Intent}.
*/
private void share() {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, mEditBody.getText().toString());
startActivity(Intent.createChooser(sharingIntent, getString(R.string.send_intent_title)));
}
}

View file

@ -0,0 +1,63 @@
/*
* Copyright (C) 2015 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.
*/
package com.example.android.directshare;
import android.content.ComponentName;
import android.content.IntentFilter;
import android.graphics.drawable.Icon;
import android.os.Bundle;
import android.service.chooser.ChooserTarget;
import android.service.chooser.ChooserTargetService;
import java.util.ArrayList;
import java.util.List;
/**
* Provides the Direct Share items to the system.
*/
public class SampleChooserTargetService extends ChooserTargetService {
@Override
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName,
IntentFilter matchedFilter) {
ComponentName componentName = new ComponentName(getPackageName(),
SendMessageActivity.class.getCanonicalName());
// The list of Direct Share items. The system will show the items the way they are sorted
// in this list.
ArrayList<ChooserTarget> targets = new ArrayList<>();
for (int i = 0; i < Contact.CONTACTS.length; ++i) {
Contact contact = Contact.byId(i);
Bundle extras = new Bundle();
extras.putInt(Contact.ID, i);
targets.add(new ChooserTarget(
// The name of this target.
contact.getName(),
// The icon to represent this target.
Icon.createWithResource(this, contact.getIcon()),
// The ranking score for this target (0.0-1.0); the system will omit items with
// low scores when there are too many Direct Share items.
0.5f,
// The name of the component to be launched if this target is chosen.
componentName,
// The extra values here will be merged into the Intent when this target is
// chosen.
extras));
}
return targets;
}
}

View file

@ -0,0 +1,98 @@
/*
* Copyright (C) 2015 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.
*/
package com.example.android.directshare;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
/**
* The dialog for selecting a contact to share the text with. This dialog is shown when the user
* taps on this sample's icon rather than any of the Direct Share contacts.
*/
public class SelectContactActivity extends Activity {
/**
* The action string for Intents.
*/
public static final String ACTION_SELECT_CONTACT
= "com.example.android.directshare.intent.action.SELECT_CONTACT";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_contact);
Intent intent = getIntent();
if (!ACTION_SELECT_CONTACT.equals(intent.getAction())) {
finish();
return;
}
// Set up the list of contacts
ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(mAdapter);
list.setOnItemClickListener(mOnItemClickListener);
}
private final ListAdapter mAdapter = new BaseAdapter() {
@Override
public int getCount() {
return Contact.CONTACTS.length;
}
@Override
public Object getItem(int i) {
return Contact.byId(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup parent) {
if (view == null) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contact, parent,
false);
}
TextView textView = (TextView) view;
Contact contact = (Contact) getItem(i);
ContactViewBinder.bind(contact, textView);
return textView;
}
};
private final AdapterView.OnItemClickListener mOnItemClickListener
= new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent data = new Intent();
data.putExtra(Contact.ID, i);
setResult(RESULT_OK, data);
finish();
}
};
}

View file

@ -0,0 +1,153 @@
/*
* Copyright (C) 2015 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.
*/
package com.example.android.directshare;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
* Provides the UI for sharing a text with a {@link Contact}.
*/
public class SendMessageActivity extends Activity {
/**
* The request code for {@link SelectContactActivity}. This is used when the user doesn't select
* any of Direct Share icons.
*/
private static final int REQUEST_SELECT_CONTACT = 1;
/**
* The text to share.
*/
private String mBody;
/**
* The ID of the contact to share the text with.
*/
private int mContactId;
// View references.
private TextView mTextContactName;
private TextView mTextMessageBody;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_message);
setTitle(R.string.sending_message);
// View references.
mTextContactName = (TextView) findViewById(R.id.contact_name);
mTextMessageBody = (TextView) findViewById(R.id.message_body);
// Resolve the share Intent.
boolean resolved = resolveIntent(getIntent());
if (!resolved) {
finish();
return;
}
// Bind event handlers.
findViewById(R.id.send).setOnClickListener(mOnClickListener);
// Set up the UI.
prepareUi();
// The contact ID will not be passed on when the user clicks on the app icon rather than any
// of the Direct Share icons. In this case, we show another dialog for selecting a contact.
if (mContactId == Contact.INVALID_ID) {
selectContact();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_SELECT_CONTACT:
if (resultCode == RESULT_OK) {
mContactId = data.getIntExtra(Contact.ID, Contact.INVALID_ID);
}
// Give up sharing the send_message if the user didn't choose a contact.
if (mContactId == Contact.INVALID_ID) {
finish();
return;
}
prepareUi();
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}
/**
* Resolves the passed {@link Intent}. This method can only resolve intents for sharing a plain
* text. {@link #mBody} and {@link #mContactId} are modified accordingly.
*
* @param intent The {@link Intent}.
* @return True if the {@code intent} is resolved properly.
*/
private boolean resolveIntent(Intent intent) {
if (Intent.ACTION_SEND.equals(intent.getAction()) &&
"text/plain".equals(intent.getType())) {
mBody = intent.getStringExtra(Intent.EXTRA_TEXT);
mContactId = intent.getIntExtra(Contact.ID, Contact.INVALID_ID);
return true;
}
return false;
}
/**
* Sets up the UI.
*/
private void prepareUi() {
if (mContactId != Contact.INVALID_ID) {
Contact contact = Contact.byId(mContactId);
ContactViewBinder.bind(contact, mTextContactName);
}
mTextMessageBody.setText(mBody);
}
/**
* Delegates selection of a {@Contact} to {@link SelectContactActivity}.
*/
private void selectContact() {
Intent intent = new Intent(this, SelectContactActivity.class);
intent.setAction(SelectContactActivity.ACTION_SELECT_CONTACT);
startActivityForResult(intent, REQUEST_SELECT_CONTACT);
}
private View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.send:
send();
break;
}
}
};
/**
* Pretends to send the text to the contact. This only shows a dummy message.
*/
private void send() {
Toast.makeText(this,
getString(R.string.message_sent, mBody, Contact.byId(mContactId).getName()),
Toast.LENGTH_SHORT).show();
finish();
}
}