1 /* 2 * Copyright (C) 2007 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.server; 18 19 import android.content.ContentResolver; 20 import android.content.ContentValues; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.res.AssetManager; 24 import android.net.Uri; 25 import android.os.Environment; 26 import android.provider.Contacts; 27 import android.provider.Settings; 28 import android.provider.MediaStore.Images; 29 import android.util.Config; 30 import android.util.Slog; 31 32 import java.io.File; 33 import java.io.FileNotFoundException; 34 import java.io.InputStream; 35 import java.io.OutputStream; 36 37 public class DemoDataSet 38 { 39 private final static String LOG_TAG = "DemoDataSet"; 40 41 private ContentResolver mContentResolver; 42 43 public final void add(Context context) 44 { 45 mContentResolver = context.getContentResolver(); 46 47 // Remove all the old data 48 mContentResolver.delete(Contacts.People.CONTENT_URI, null, null); 49 50 // Add the new data 51 addDefaultData(); 52 53 // Add images from /android/images 54 addDefaultImages(); 55 } 56 57 private final void addDefaultImages() 58 { 59 File rootDirectory = Environment.getRootDirectory(); 60 String [] files 61 = new File(rootDirectory, "images").list(); 62 int count = files.length; 63 64 if (count == 0) { 65 Slog.i(LOG_TAG, "addDefaultImages: no images found!"); 66 return; 67 } 68 69 for (int i = 0; i < count; i++) 70 { 71 String name = files[i]; 72 String path = rootDirectory + "/" + name; 73 74 try { 75 Images.Media.insertImage(mContentResolver, path, name, null); 76 } catch (FileNotFoundException e) { 77 Slog.e(LOG_TAG, "Failed to import image " + path, e); 78 } 79 } 80 } 81 82 private final void addDefaultData() 83 { 84 Slog.i(LOG_TAG, "Adding default data..."); 85 86 // addImage("Violet", "images/violet.png"); 87 // addImage("Corky", "images/corky.png"); 88 89 // PENDING: should this be done here?!?! 90 Intent intent = new Intent( 91 Intent.ACTION_CALL, Uri.fromParts("voicemail", "", null)); 92 addShortcut("1", intent); 93 } 94 95 private final Uri addImage(String name, Uri file) 96 { 97 ContentValues imagev = new ContentValues(); 98 imagev.put("name", name); 99 100 Uri url = null; 101 102 AssetManager ass = AssetManager.getSystem(); 103 InputStream in = null; 104 OutputStream out = null; 105 106 try 107 { 108 in = ass.open(file.toString()); 109 110 url = mContentResolver.insert(Images.Media.INTERNAL_CONTENT_URI, imagev); 111 out = mContentResolver.openOutputStream(url); 112 113 final int size = 8 * 1024; 114 byte[] buf = new byte[size]; 115 116 int count = 0; 117 do 118 { 119 count = in.read(buf, 0, size); 120 if (count > 0) { 121 out.write(buf, 0, count); 122 } 123 } while (count > 0); 124 } 125 catch (Exception e) 126 { 127 Slog.e(LOG_TAG, "Failed to insert image '" + file + "'", e); 128 url = null; 129 } 130 131 return url; 132 } 133 134 private final Uri addShortcut(String shortcut, Intent intent) 135 { 136 if (Config.LOGV) Slog.v(LOG_TAG, "addShortcut: shortcut=" + shortcut + ", intent=" + intent); 137 return Settings.Bookmarks.add(mContentResolver, intent, null, null, 138 shortcut != null ? shortcut.charAt(0) : 0, 0); 139 } 140 } 141