blob: 0de7c1e7532a166f63828d59d59ba9d80d13ec7e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.server;
18
19import android.content.ContentResolver;
20import android.content.ContentValues;
21import android.content.Context;
22import android.content.Intent;
23import android.content.res.AssetManager;
24import android.net.Uri;
25import android.os.Environment;
26import android.provider.Contacts;
27import android.provider.Settings;
28import android.provider.MediaStore.Images;
29import android.util.Config;
30import android.util.Log;
31
32import java.io.File;
33import java.io.FileNotFoundException;
34import java.io.InputStream;
35import java.io.OutputStream;
36
37public 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 Log.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 Log.e(LOG_TAG, "Failed to import image " + path, e);
78 }
79 }
80 }
81
82 private final void addDefaultData()
83 {
84 Log.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 Log.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) Log.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}