Add browseable samples for L SDK release

Change-Id: I71c6ff9a90b7734042d68af7f01e6d61118cc508
This commit is contained in:
Trevor Johns
2014-10-17 03:31:00 -07:00
parent 66b6f721b7
commit 6876a9cfc5
666 changed files with 28858 additions and 0 deletions

View File

@@ -0,0 +1,134 @@
/*
* Copyright 2014 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.lnotifications;
import android.app.Fragment;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
/**
* Fragment that demonstrates options for displaying Heads-Up Notifications.
*/
public class HeadsUpNotificationFragment extends Fragment {
/**
* NotificationId used for the notifications from this Fragment.
*/
private static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
/**
* Button to show a notification.
*/
private Button mShowNotificationButton;
/**
* If checked, notifications that this Fragment creates will be displayed as Heads-Up
* Notifications.
*/
private CheckBox mUseHeadsUpCheckbox;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment NotificationFragment.
*/
public static HeadsUpNotificationFragment newInstance() {
HeadsUpNotificationFragment fragment = new HeadsUpNotificationFragment();
fragment.setRetainInstance(true);
return fragment;
}
public HeadsUpNotificationFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNotificationManager = (NotificationManager) getActivity().getSystemService(Context
.NOTIFICATION_SERVICE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_heads_up_notification, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mShowNotificationButton = (Button) view.findViewById(R.id.show_notification_button);
mShowNotificationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mNotificationManager.notify(NOTIFICATION_ID, createNotification(
mUseHeadsUpCheckbox.isChecked()));
Toast.makeText(getActivity(), "Show Notification clicked", Toast.LENGTH_SHORT).show();
}
});
mUseHeadsUpCheckbox = (CheckBox) view.findViewById(R.id.use_heads_up_checkbox);
}
/**
* Creates a new notification depending on the argument.
*
* @param makeHeadsUpNotification A boolean value to indicating whether a notification will be
* created as a heads-up notification or not.
* <ul>
* <li>true : Creates a heads-up notification.</li>
* <li>false : Creates a non-heads-up notification.</li>
* </ul>
*
* @return A Notification instance.
*/
//@VisibleForTesting
Notification createNotification(boolean makeHeadsUpNotification) {
Notification.Builder notificationBuilder = new Notification.Builder(getActivity())
.setSmallIcon(R.drawable.ic_launcher_notification)
.setPriority(Notification.PRIORITY_DEFAULT)
.setCategory(Notification.CATEGORY_MESSAGE)
.setContentTitle("Sample Notification")
.setContentText("This is a normal notification.");
if (makeHeadsUpNotification) {
Intent push = new Intent();
push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
push.setClass(getActivity(), LNotificationActivity.class);
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(getActivity(), 0,
push, PendingIntent.FLAG_CANCEL_CURRENT);
notificationBuilder
.setContentText("Heads-Up Notification on Android L or above.")
.setFullScreenIntent(fullScreenPendingIntent, true);
}
return notificationBuilder.build();
}
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright 2014 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.lnotifications;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
/**
* Launcher Activity for the L Notification samples application.
*/
public class LNotificationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
setTitle(R.string.title_lnotification_activity);
ActionBar actionBar = getActionBar();
// Use ViewPager in the support library where possible.
// At this time, the support library for L is not ready so using the deprecated method
// to create tabs.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tabHeadsUpNotification = actionBar.newTab().setText("Heads Up");
ActionBar.Tab tabVisibilityMetadata = actionBar.newTab().setText("Visibility");
ActionBar.Tab tabOtherMetadata = actionBar.newTab().setText("Others");
tabHeadsUpNotification.setTabListener(new FragmentTabListener(HeadsUpNotificationFragment
.newInstance()));
tabVisibilityMetadata.setTabListener(new FragmentTabListener(VisibilityMetadataFragment
.newInstance()));
tabOtherMetadata.setTabListener(new FragmentTabListener(OtherMetadataFragment.newInstance
()));
actionBar.addTab(tabHeadsUpNotification, 0);
actionBar.addTab(tabVisibilityMetadata, 1);
actionBar.addTab(tabOtherMetadata, 2);
}
/**
* TabListener that replaces a Fragment when a tab is clicked.
*/
private static class FragmentTabListener implements ActionBar.TabListener {
public Fragment fragment;
public FragmentTabListener(Fragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
//do nothing.
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
ft.replace(R.id.container, fragment);
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
}

View File

@@ -0,0 +1,345 @@
/*
* Copyright 2014 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.lnotifications;
import android.app.Activity;
import android.app.Fragment;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.Random;
/**
* Fragment that demonstrates how to attach metadata introduced in Android L, such as
* priority data, notification category and person data.
*/
public class OtherMetadataFragment extends Fragment {
private static final String TAG = OtherMetadataFragment.class.getSimpleName();
/**
* Request code used for picking a contact.
*/
public static final int REQUEST_CODE_PICK_CONTACT = 1;
/**
* Incremental Integer used for ID for notifications so that each notification will be
* treated differently.
*/
private Integer mIncrementalNotificationId = Integer.valueOf(0);
private NotificationManager mNotificationManager;
/**
* Button to show a notification.
*/
private Button mShowNotificationButton;
/**
* Spinner that holds possible categories used for a notification as
* {@link Notification.Builder#setCategory(String)}.
*/
private Spinner mCategorySpinner;
/**
* Spinner that holds possible priorities used for a notification as
* {@link Notification.Builder#setPriority(int)}.
*/
private Spinner mPrioritySpinner;
/**
* Holds a URI for the person to be attached to the notification.
*/
//@VisibleForTesting
Uri mContactUri;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment NotificationFragment.
*/
public static OtherMetadataFragment newInstance() {
OtherMetadataFragment fragment = new OtherMetadataFragment();
fragment.setRetainInstance(true);
return fragment;
}
public OtherMetadataFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNotificationManager = (NotificationManager) getActivity().getSystemService(Context
.NOTIFICATION_SERVICE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_other_metadata, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mShowNotificationButton = (Button) view.findViewById(R.id.show_notification_button);
mShowNotificationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Priority selectedPriority = (Priority) mPrioritySpinner.getSelectedItem();
Category selectedCategory = (Category) mCategorySpinner.getSelectedItem();
showNotificationClicked(selectedPriority, selectedCategory, mContactUri);
}
});
mCategorySpinner = (Spinner) view.findViewById(R.id.category_spinner);
ArrayAdapter<Category> categoryArrayAdapter = new ArrayAdapter<Category>(getActivity(),
android.R.layout.simple_spinner_item, Category.values());
categoryArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mCategorySpinner.setAdapter(categoryArrayAdapter);
mPrioritySpinner = (Spinner) view.findViewById(R.id.priority_spinner);
ArrayAdapter<Priority> priorityArrayAdapter = new ArrayAdapter<Priority>(getActivity(),
android.R.layout.simple_spinner_item, Priority.values());
priorityArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mPrioritySpinner.setAdapter(priorityArrayAdapter);
view.findViewById(R.id.attach_person).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
findContact();
}
});
view.findViewById(R.id.contact_entry).setVisibility(View.GONE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CODE_PICK_CONTACT:
if (resultCode == Activity.RESULT_OK) {
Uri contactUri = data.getData();
mContactUri = contactUri;
updateContactEntryFromUri(contactUri);
}
break;
}
}
/**
* Invoked when {@link #mShowNotificationButton} is clicked.
* Creates a new notification and sets metadata passed as arguments.
*
* @param priority The priority metadata.
* @param category The category metadata.
* @param contactUri The URI to be added to the new notification as metadata.
*
* @return A Notification instance.
*/
//@VisibleForTesting
Notification createNotification(Priority priority, Category category, Uri contactUri) {
Notification.Builder notificationBuilder = new Notification.Builder(getActivity())
.setContentTitle("Notification with other metadata")
.setSmallIcon(R.drawable.ic_launcher_notification)
.setPriority(priority.value)
.setCategory(category.value)
.setContentText(String.format("Category %s, Priority %s", category.value,
priority.name()));
if (contactUri != null) {
notificationBuilder.addPerson(contactUri.toString());
Bitmap photoBitmap = loadBitmapFromContactUri(contactUri);
if (photoBitmap != null) {
notificationBuilder.setLargeIcon(photoBitmap);
}
}
return notificationBuilder.build();
}
/**
* Invoked when {@link #mShowNotificationButton} is clicked.
* Creates a new notification and sets metadata passed as arguments.
*
* @param priority The priority metadata.
* @param category The category metadata.
* @param contactUri The URI to be added to the new notification as metadata.
*/
private void showNotificationClicked(Priority priority, Category category, Uri contactUri) {
// Assigns a unique (incremented) notification ID in order to treat each notification as a
// different one. This helps demonstrate how a priority flag affects ordering.
mIncrementalNotificationId = new Integer(mIncrementalNotificationId + 1);
mNotificationManager.notify(mIncrementalNotificationId, createNotification(priority,
category, contactUri));
Toast.makeText(getActivity(), "Show Notification clicked", Toast.LENGTH_SHORT).show();
}
private void findContact() {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE_PICK_CONTACT);
}
/**
* Returns a {@link Bitmap} from the Uri specified as the argument.
*
* @param contactUri The Uri from which the result Bitmap is created.
* @return The {@link Bitmap} instance retrieved from the contactUri.
*/
private Bitmap loadBitmapFromContactUri(Uri contactUri) {
if (contactUri == null) {
return null;
}
Bitmap result = null;
Cursor cursor = getActivity().getContentResolver().query(contactUri, null, null, null,
null);
if (cursor != null && cursor.moveToFirst()) {
int idx = cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID);
String hasPhoto = cursor.getString(idx);
Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo
.CONTENT_DIRECTORY);
if (hasPhoto != null) {
try {
result = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver()
, photoUri);
} catch (IOException e) {
Log.e(TAG, String.format("Failed to load resource. Uri %s", photoUri), e);
}
} else {
Drawable defaultContactDrawable = getActivity().getResources().getDrawable(R
.drawable.ic_contact_picture);
result = ((BitmapDrawable) defaultContactDrawable).getBitmap();
}
}
return result;
}
/**
* Updates the Contact information on the screen when a contact is picked.
*
* @param contactUri The Uri from which the contact is retrieved.
*/
private void updateContactEntryFromUri(Uri contactUri) {
Cursor cursor = getActivity().getContentResolver().query(contactUri, null, null, null,
null);
if (cursor != null && cursor.moveToFirst()) {
int idx = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String name = cursor.getString(idx);
idx = cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID);
String hasPhoto = cursor.getString(idx);
Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo
.CONTENT_DIRECTORY);
ImageView contactPhoto = (ImageView) getActivity().findViewById(R.id.contact_photo);
if (hasPhoto != null) {
contactPhoto.setImageURI(photoUri);
} else {
Drawable defaultContactDrawable = getActivity().getResources().getDrawable(R
.drawable.ic_contact_picture);
contactPhoto.setImageDrawable(defaultContactDrawable);
}
TextView contactName = (TextView) getActivity().findViewById(R.id.contact_name);
contactName.setText(name);
getActivity().findViewById(R.id.contact_entry).setVisibility(View.VISIBLE);
getActivity().findViewById(R.id.attach_person).setVisibility(View.GONE);
getActivity().findViewById(R.id.click_to_change).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
findContact();
}
});
Log.i(TAG, String.format("Contact updated. Name %s, PhotoUri %s", name, photoUri));
}
}
/**
* Enum indicating possible categories in {@link Notification} used from
* {@link #mCategorySpinner}.
*/
//@VisibleForTesting
static enum Category {
ALARM("alarm"),
CALL("call"),
EMAIL("email"),
ERROR("err"),
EVENT("event"),
MESSAGE("msg"),
PROGRESS("progress"),
PROMO("promo"),
RECOMMENDATION("recommendation"),
SERVICE("service"),
SOCIAL("social"),
STATUS("status"),
SYSTEM("sys"),
TRANSPORT("transport");
private final String value;
Category(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}
/**
* Enum indicating possible priorities in {@link Notification} used from
* {@link #mPrioritySpinner}.
*/
//@VisibleForTesting
static enum Priority {
DEFAULT(Notification.PRIORITY_DEFAULT),
MAX(Notification.PRIORITY_MAX),
HIGH(Notification.PRIORITY_HIGH),
LOW(Notification.PRIORITY_LOW),
MIN(Notification.PRIORITY_MIN);
private final int value;
Priority(int value) {
this.value = value;
}
}
}

View File

@@ -0,0 +1,202 @@
/*
* Copyright 2014 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.lnotifications;
import android.app.Fragment;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.util.Random;
/**
* Fragment that demonstrates how notifications with different visibility metadata differ on
* a lockscreen.
*/
public class VisibilityMetadataFragment extends Fragment {
private NotificationManager mNotificationManager;
/**
* {@link RadioGroup} that has Visibility RadioButton in its children.
*/
private RadioGroup mRadioGroup;
/**
* Incremental Integer used for ID for notifications so that each notification will be
* treated differently.
*/
private Integer mIncrementalNotificationId = Integer.valueOf(0);
/**
* Button to show a notification.
*/
private Button mShowNotificationButton;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment NotificationFragment.
*/
public static VisibilityMetadataFragment newInstance() {
VisibilityMetadataFragment fragment = new VisibilityMetadataFragment();
fragment.setRetainInstance(true);
return fragment;
}
public VisibilityMetadataFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNotificationManager = (NotificationManager) getActivity().getSystemService(Context
.NOTIFICATION_SERVICE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_visibility_metadata_notification, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mShowNotificationButton = (Button) view.findViewById(R.id.show_notification_button);
mShowNotificationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NotificationVisibility visibility = getVisibilityFromSelectedRadio(mRadioGroup);
showNotificationClicked(visibility);
}
});
mRadioGroup = (RadioGroup) view.findViewById(R.id.visibility_radio_group);
}
/**
* Creates a new notification with a different visibility level.
*
* @param visibility The visibility of the notification to be created.
*
* @return A Notification instance.
*/
//@VisibleForTesting
Notification createNotification(NotificationVisibility visibility) {
Notification.Builder notificationBuilder = new Notification.Builder(getActivity())
.setContentTitle("Notification for Visibility metadata");
notificationBuilder.setVisibility(visibility.getVisibility());
notificationBuilder.setContentText(String.format("Visibility : %s",
visibility.getDescription()));
notificationBuilder.setSmallIcon(visibility.getNotificationIconId());
return notificationBuilder.build();
}
/**
* Returns a {@link NotificationVisibility} depending on which RadioButton in the radiogroup
* is selected.
*
* @param radiogroup The RadioGroup.
* @return The instance of {@link NotificationVisibility} corresponding to RadioButton.
*/
private NotificationVisibility getVisibilityFromSelectedRadio(RadioGroup radiogroup) {
switch (radiogroup.getCheckedRadioButtonId()) {
case R.id.visibility_public_radio_button:
return NotificationVisibility.PUBLIC;
case R.id.visibility_private_radio_button:
return NotificationVisibility.PRIVATE;
case R.id.visibility_secret_radio_button:
return NotificationVisibility.SECRET;
default:
//If not selected, returns PUBLIC as default.
return NotificationVisibility.PUBLIC;
}
}
/**
* Invoked when {@link #mShowNotificationButton} is clicked.
* Creates a new notification with a different visibility level.
*
* @param visibility The visibility of the notification to be created.
*/
private void showNotificationClicked(NotificationVisibility visibility) {
// Assigns a unique (incremented) notification ID in order to treat each notification as a
// different one. This helps demonstrate how a notification with a different visibility
// level differs on the lockscreen.
mIncrementalNotificationId = new Integer(mIncrementalNotificationId + 1);
mNotificationManager.notify(mIncrementalNotificationId, createNotification(visibility));
Toast.makeText(getActivity(), "Show Notification clicked", Toast.LENGTH_SHORT).show();
}
/**
* Enum indicating possible visibility levels for notifications and related data(String
* representation of visibility levels, an icon ID to create a notification) to
* create a notification.
*/
//@VisibleForTesting
static enum NotificationVisibility {
PUBLIC(Notification.VISIBILITY_PUBLIC, "Public", R.drawable.ic_public_notification),
PRIVATE(Notification.VISIBILITY_PRIVATE, "Private", R.drawable.ic_private_notification),
SECRET(Notification.VISIBILITY_SECRET, "Secret", R.drawable.ic_secret_notification);
/**
* Visibility level of the notification.
*/
private final int mVisibility;
/**
* String representation of the visibility.
*/
private final String mDescription;
/**
* Id of an icon used for notifications created from the visibility.
*/
private final int mNotificationIconId;
NotificationVisibility(int visibility, String description, int notificationIconId) {
mVisibility = visibility;
mDescription = description;
mNotificationIconId = notificationIconId;
}
public int getVisibility() {
return mVisibility;
}
public String getDescription() {
return mDescription;
}
public int getNotificationIconId() {
return mNotificationIconId;
}
}
}