Home | History | Annotate | Download | only in com.example.android.lnotifications
      1 /*
      2 * Copyright 2014 The Android Open Source Project
      3 *
      4 * Licensed under the Apache License, Version 2.0 (the "License");
      5 * you may not use this file except in compliance with the License.
      6 * You may obtain a copy of the License at
      7 *
      8 *     http://www.apache.org/licenses/LICENSE-2.0
      9 *
     10 * Unless required by applicable law or agreed to in writing, software
     11 * distributed under the License is distributed on an "AS IS" BASIS,
     12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 * See the License for the specific language governing permissions and
     14 * limitations under the License.
     15 */
     16 
     17 package com.example.android.lnotifications;
     18 
     19 import android.app.Activity;
     20 import android.app.Notification;
     21 import android.app.NotificationManager;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.database.Cursor;
     25 import android.graphics.Bitmap;
     26 import android.graphics.drawable.BitmapDrawable;
     27 import android.graphics.drawable.Drawable;
     28 import android.net.Uri;
     29 import android.os.Bundle;
     30 import android.provider.ContactsContract;
     31 import android.provider.MediaStore;
     32 import android.support.v4.app.Fragment;
     33 import android.util.Log;
     34 import android.view.LayoutInflater;
     35 import android.view.View;
     36 import android.view.ViewGroup;
     37 import android.widget.ArrayAdapter;
     38 import android.widget.Button;
     39 import android.widget.ImageView;
     40 import android.widget.Spinner;
     41 import android.widget.TextView;
     42 import android.widget.Toast;
     43 
     44 import java.io.IOException;
     45 
     46 /**
     47  * Fragment that demonstrates how to attach metadata introduced in Android L, such as
     48  * priority data, notification category and person data.
     49  */
     50 public class OtherMetadataFragment extends Fragment {
     51 
     52     private static final String TAG = OtherMetadataFragment.class.getSimpleName();
     53 
     54     /**
     55      * Request code used for picking a contact.
     56      */
     57     public static final int REQUEST_CODE_PICK_CONTACT = 1;
     58 
     59     /**
     60      * Incremental int used for ID for notifications so that each notification will be
     61      * treated differently.
     62      */
     63     private int mIncrementalNotificationId = 0;
     64 
     65     private NotificationManager mNotificationManager;
     66 
     67     /**
     68      * Button to show a notification.
     69      */
     70     private Button mShowNotificationButton;
     71 
     72     /**
     73      *  Spinner that holds possible categories used for a notification as
     74      *  {@link Notification.Builder#setCategory(String)}.
     75      */
     76     private Spinner mCategorySpinner;
     77 
     78     /**
     79      * Spinner that holds possible priorities used for a notification as
     80      * {@link Notification.Builder#setPriority(int)}.
     81      */
     82     private Spinner mPrioritySpinner;
     83 
     84     /**
     85      * Holds a URI for the person to be attached to the notification.
     86      */
     87     private Uri mContactUri;
     88 
     89     /**
     90      * Use this factory method to create a new instance of
     91      * this fragment using the provided parameters.
     92      *
     93      * @return A new instance of fragment NotificationFragment.
     94      */
     95     public static OtherMetadataFragment newInstance() {
     96         OtherMetadataFragment fragment = new OtherMetadataFragment();
     97         fragment.setRetainInstance(true);
     98         return fragment;
     99     }
    100 
    101     public OtherMetadataFragment() {
    102         // Required empty public constructor
    103     }
    104 
    105     @Override
    106     public void onCreate(Bundle savedInstanceState) {
    107         super.onCreate(savedInstanceState);
    108         mNotificationManager = (NotificationManager) getActivity().getSystemService(Context
    109                 .NOTIFICATION_SERVICE);
    110     }
    111 
    112     @Override
    113     public View onCreateView(LayoutInflater inflater, ViewGroup container,
    114                              Bundle savedInstanceState) {
    115         // Inflate the layout for this fragment
    116         return inflater.inflate(R.layout.fragment_other_metadata, container, false);
    117     }
    118 
    119     @Override
    120     public void onViewCreated(View view, Bundle savedInstanceState) {
    121         super.onViewCreated(view, savedInstanceState);
    122         mShowNotificationButton = (Button) view.findViewById(R.id.show_notification_button);
    123         mShowNotificationButton.setOnClickListener(new View.OnClickListener() {
    124             @Override
    125             public void onClick(View view) {
    126                 Priority selectedPriority = (Priority) mPrioritySpinner.getSelectedItem();
    127                 Category selectedCategory = (Category) mCategorySpinner.getSelectedItem();
    128                 showNotificationClicked(selectedPriority, selectedCategory, mContactUri);
    129             }
    130         });
    131 
    132         mCategorySpinner = (Spinner) view.findViewById(R.id.category_spinner);
    133         ArrayAdapter<Category> categoryArrayAdapter = new ArrayAdapter<Category>(getActivity(),
    134                 android.R.layout.simple_spinner_item, Category.values());
    135         categoryArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    136         mCategorySpinner.setAdapter(categoryArrayAdapter);
    137 
    138         mPrioritySpinner = (Spinner) view.findViewById(R.id.priority_spinner);
    139         ArrayAdapter<Priority> priorityArrayAdapter = new ArrayAdapter<Priority>(getActivity(),
    140                 android.R.layout.simple_spinner_item, Priority.values());
    141         priorityArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    142         mPrioritySpinner.setAdapter(priorityArrayAdapter);
    143 
    144         view.findViewById(R.id.attach_person).setOnClickListener(new View.OnClickListener() {
    145             @Override
    146             public void onClick(View view) {
    147                 findContact();
    148             }
    149         });
    150 
    151         view.findViewById(R.id.contact_entry).setVisibility(View.GONE);
    152     }
    153 
    154     @Override
    155     public void onActivityResult(int requestCode, int resultCode, Intent data) {
    156         super.onActivityResult(requestCode, resultCode, data);
    157         switch (requestCode) {
    158             case REQUEST_CODE_PICK_CONTACT:
    159                 if (resultCode == Activity.RESULT_OK) {
    160                     Uri contactUri = data.getData();
    161                     mContactUri = contactUri;
    162                     updateContactEntryFromUri(contactUri);
    163                 }
    164                 break;
    165         }
    166     }
    167 
    168     /**
    169      * Invoked when {@link #mShowNotificationButton} is clicked.
    170      * Creates a new notification and sets metadata passed as arguments.
    171      *
    172      * @param priority   The priority metadata.
    173      * @param category   The category metadata.
    174      * @param contactUri The URI to be added to the new notification as metadata.
    175      *
    176      * @return A Notification instance.
    177      */
    178     private Notification createNotification(Priority priority, Category category, Uri contactUri) {
    179         Notification.Builder notificationBuilder = new Notification.Builder(getActivity())
    180                 .setContentTitle("Notification with other metadata")
    181                 .setSmallIcon(R.drawable.ic_launcher_notification)
    182                 .setPriority(priority.value)
    183                 .setCategory(category.value)
    184                 .setContentText(String.format("Category %s, Priority %s", category.value,
    185                         priority.name()));
    186         if (contactUri != null) {
    187             notificationBuilder.addPerson(contactUri.toString());
    188             Bitmap photoBitmap = loadBitmapFromContactUri(contactUri);
    189             if (photoBitmap != null) {
    190                 notificationBuilder.setLargeIcon(photoBitmap);
    191             }
    192         }
    193         return notificationBuilder.build();
    194     }
    195 
    196     /**
    197      * Invoked when {@link #mShowNotificationButton} is clicked.
    198      * Creates a new notification and sets metadata passed as arguments.
    199      *
    200      * @param priority   The priority metadata.
    201      * @param category   The category metadata.
    202      * @param contactUri The URI to be added to the new notification as metadata.
    203      */
    204     private void showNotificationClicked(Priority priority, Category category, Uri contactUri) {
    205         // Assigns a unique (incremented) notification ID in order to treat each notification as a
    206         // different one. This helps demonstrate how a priority flag affects ordering.
    207         mIncrementalNotificationId++;
    208         mNotificationManager.notify(mIncrementalNotificationId, createNotification(priority,
    209                 category, contactUri));
    210         Toast.makeText(getActivity(), "Show Notification clicked", Toast.LENGTH_SHORT).show();
    211     }
    212 
    213     private void findContact() {
    214         Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    215         startActivityForResult(intent, REQUEST_CODE_PICK_CONTACT);
    216     }
    217 
    218     /**
    219      * Returns a {@link Bitmap} from the Uri specified as the argument.
    220      *
    221      * @param contactUri The Uri from which the result Bitmap is created.
    222      * @return The {@link Bitmap} instance retrieved from the contactUri.
    223      */
    224     private Bitmap loadBitmapFromContactUri(Uri contactUri) {
    225         if (contactUri == null) {
    226             return null;
    227         }
    228         Bitmap result = null;
    229         Cursor cursor = getActivity().getContentResolver().query(contactUri, null, null, null,
    230                 null);
    231         if (cursor != null && cursor.moveToFirst()) {
    232             int idx = cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID);
    233             String hasPhoto = cursor.getString(idx);
    234             Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo
    235                     .CONTENT_DIRECTORY);
    236             if (hasPhoto != null) {
    237                 try {
    238                     result = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver()
    239                             , photoUri);
    240                 } catch (IOException e) {
    241                     Log.e(TAG, String.format("Failed to load resource. Uri %s", photoUri), e);
    242                 }
    243             } else {
    244                 Drawable defaultContactDrawable = getActivity().getResources().getDrawable(R
    245                         .drawable.ic_contact_picture);
    246                 result = ((BitmapDrawable) defaultContactDrawable).getBitmap();
    247             }
    248         }
    249         return result;
    250     }
    251 
    252     /**
    253      * Updates the Contact information on the screen when a contact is picked.
    254      *
    255      * @param contactUri The Uri from which the contact is retrieved.
    256      */
    257     private void updateContactEntryFromUri(Uri contactUri) {
    258         Cursor cursor = getActivity().getContentResolver().query(contactUri, null, null, null,
    259                 null);
    260         if (cursor != null && cursor.moveToFirst()) {
    261             int idx = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
    262             String name = cursor.getString(idx);
    263             idx = cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID);
    264             String hasPhoto = cursor.getString(idx);
    265 
    266             Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo
    267                     .CONTENT_DIRECTORY);
    268             ImageView contactPhoto = (ImageView) getActivity().findViewById(R.id.contact_photo);
    269             if (hasPhoto != null) {
    270                 contactPhoto.setImageURI(photoUri);
    271             } else {
    272                 Drawable defaultContactDrawable = getActivity().getResources().getDrawable(R
    273                         .drawable.ic_contact_picture);
    274                 contactPhoto.setImageDrawable(defaultContactDrawable);
    275             }
    276             TextView contactName = (TextView) getActivity().findViewById(R.id.contact_name);
    277             contactName.setText(name);
    278 
    279             getActivity().findViewById(R.id.contact_entry).setVisibility(View.VISIBLE);
    280             getActivity().findViewById(R.id.attach_person).setVisibility(View.GONE);
    281             getActivity().findViewById(R.id.click_to_change).setOnClickListener(new View.OnClickListener() {
    282                 @Override
    283                 public void onClick(View view) {
    284                     findContact();
    285                 }
    286             });
    287             Log.i(TAG, String.format("Contact updated. Name %s, PhotoUri %s", name, photoUri));
    288         }
    289     }
    290 
    291     /**
    292      * Enum indicating possible categories in {@link Notification} used from
    293      * {@link #mCategorySpinner}.
    294      */
    295     private enum Category {
    296         ALARM("alarm"),
    297         CALL("call"),
    298         EMAIL("email"),
    299         ERROR("err"),
    300         EVENT("event"),
    301         MESSAGE("msg"),
    302         PROGRESS("progress"),
    303         PROMO("promo"),
    304         RECOMMENDATION("recommendation"),
    305         SERVICE("service"),
    306         SOCIAL("social"),
    307         STATUS("status"),
    308         SYSTEM("sys"),
    309         TRANSPORT("transport");
    310 
    311         private final String value;
    312 
    313         Category(String value) {
    314             this.value = value;
    315         }
    316 
    317         @Override
    318         public String toString() {
    319             return value;
    320         }
    321     }
    322 
    323     /**
    324      * Enum indicating possible priorities in {@link Notification} used from
    325      * {@link #mPrioritySpinner}.
    326      */
    327     private enum Priority {
    328         DEFAULT(Notification.PRIORITY_DEFAULT),
    329         MAX(Notification.PRIORITY_MAX),
    330         HIGH(Notification.PRIORITY_HIGH),
    331         LOW(Notification.PRIORITY_LOW),
    332         MIN(Notification.PRIORITY_MIN);
    333 
    334         private final int value;
    335 
    336         Priority(int value) {
    337             this.value = value;
    338         }
    339     }
    340 }
    341