Home | History | Annotate | Download | only in externalstorageapp
      1 /*
      2  * Copyright (C) 2017 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.android.cts.externalstorageapp;
     18 
     19 import android.content.Context;
     20 import android.media.AudioAttributes;
     21 import android.media.AudioManager;
     22 import android.media.MediaPlayer;
     23 import android.net.Uri;
     24 import android.provider.Settings;
     25 import android.test.AndroidTestCase;
     26 
     27 /** Verify system default URI's can be read without READ_EXTERNAL_STORAGE permission. */
     28 public class ReadDefaultUris extends AndroidTestCase {
     29 
     30     private AudioManager mAudioManager;
     31 
     32     @Override
     33     protected void setUp() throws Exception {
     34         super.setUp();
     35         mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
     36     }
     37 
     38     public void testPlayDefaultUris() throws Exception {
     39         final long timeToPlayMs = 1000;
     40         playUri(
     41                 Settings.System.DEFAULT_NOTIFICATION_URI,
     42                 timeToPlayMs,
     43                 AudioAttributes.USAGE_NOTIFICATION,
     44                 AudioAttributes.CONTENT_TYPE_SONIFICATION);
     45         playUri(
     46                 Settings.System.DEFAULT_RINGTONE_URI,
     47                 timeToPlayMs,
     48                 AudioAttributes.USAGE_NOTIFICATION_RINGTONE,
     49                 AudioAttributes.CONTENT_TYPE_SONIFICATION);
     50         playUri(
     51                 Settings.System.DEFAULT_ALARM_ALERT_URI,
     52                 timeToPlayMs,
     53                 AudioAttributes.USAGE_ALARM,
     54                 AudioAttributes.CONTENT_TYPE_SONIFICATION);
     55     }
     56 
     57     private void playUri(final Uri uri, long timeToPlayMs, int usage, int contentType)
     58             throws Exception {
     59         MediaPlayer mp = new MediaPlayer();
     60         assertNotNull(mp);
     61         mp.setDataSource(mContext, uri);
     62         mp.setAudioAttributes(
     63                 new AudioAttributes.Builder().setUsage(usage).setContentType(contentType).build());
     64         mp.prepare();
     65         mp.start();
     66         Thread.sleep(timeToPlayMs);
     67         mp.stop();
     68         mp.release();
     69         Thread.sleep(timeToPlayMs);
     70     }
     71 }
     72