1 /*
2  * Copyright (C) 2008 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.launcher3;
18 
19 import android.content.ContentValues;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.res.XmlResourceParser;
23 import android.database.sqlite.SQLiteDatabase;
24 import android.util.Log;
25 
26 import com.android.launcher3.AutoInstallsLayout.LayoutParserCallback;
27 import com.android.launcher3.LauncherSettings.Favorites;
28 import com.android.launcher3.backup.nano.BackupProtos.Favorite;
29 import com.android.launcher3.util.Thunk;
30 
31 import org.xmlpull.v1.XmlPullParserException;
32 
33 import java.io.IOException;
34 
35 /**
36  * A class that parses content values corresponding to some common app types.
37  */
38 public class CommonAppTypeParser implements LayoutParserCallback {
39     private static final String TAG = "CommonAppTypeParser";
40 
41     // Including TARGET_NONE
42     public static final int SUPPORTED_TYPE_COUNT = 7;
43 
44     private static final int RESTORE_FLAG_BIT_SHIFT = 4;
45 
46 
47     private final long mItemId;
48     @Thunk final int mResId;
49     @Thunk final Context mContext;
50 
51     ContentValues parsedValues;
52     Intent parsedIntent;
53     String parsedTitle;
54 
CommonAppTypeParser(long itemId, int itemType, Context context)55     public CommonAppTypeParser(long itemId, int itemType, Context context) {
56         mItemId = itemId;
57         mContext = context;
58         mResId = getResourceForItemType(itemType);
59     }
60 
61     @Override
generateNewItemId()62     public long generateNewItemId() {
63         return mItemId;
64     }
65 
66     @Override
insertAndCheck(SQLiteDatabase db, ContentValues values)67     public long insertAndCheck(SQLiteDatabase db, ContentValues values) {
68         parsedValues = values;
69 
70         // Remove unwanted values
71         values.put(Favorites.ICON_TYPE, (Integer) null);
72         values.put(Favorites.ICON_PACKAGE, (String) null);
73         values.put(Favorites.ICON_RESOURCE, (String) null);
74         values.put(Favorites.ICON, (byte[]) null);
75         return 1;
76     }
77 
78     /**
79      * Tries to find a suitable app to the provided app type.
80      */
findDefaultApp()81     public boolean findDefaultApp() {
82         if (mResId == 0) {
83             return false;
84         }
85 
86         parsedIntent = null;
87         parsedValues = null;
88         new MyLayoutParser().parseValues();
89         return (parsedValues != null) && (parsedIntent != null);
90     }
91 
92     private class MyLayoutParser extends DefaultLayoutParser {
93 
MyLayoutParser()94         public MyLayoutParser() {
95             super(CommonAppTypeParser.this.mContext, null, CommonAppTypeParser.this,
96                     CommonAppTypeParser.this.mContext.getResources(), mResId, TAG_RESOLVE);
97         }
98 
99         @Override
addShortcut(String title, Intent intent, int type)100         protected long addShortcut(String title, Intent intent, int type) {
101             if (type == Favorites.ITEM_TYPE_APPLICATION) {
102                 parsedIntent = intent;
103                 parsedTitle = title;
104             }
105             return super.addShortcut(title, intent, type);
106         }
107 
parseValues()108         public void parseValues() {
109             XmlResourceParser parser = mSourceRes.getXml(mLayoutId);
110             try {
111                 beginDocument(parser, mRootTag);
112                 new ResolveParser().parseAndAdd(parser);
113             } catch (IOException | XmlPullParserException e) {
114                 Log.e(TAG, "Unable to parse default app info", e);
115             }
116             parser.close();
117         }
118     }
119 
getResourceForItemType(int type)120     public static int getResourceForItemType(int type) {
121         switch (type) {
122             case Favorite.TARGET_PHONE:
123                 return R.xml.app_target_phone;
124 
125             case Favorite.TARGET_MESSENGER:
126                 return R.xml.app_target_messenger;
127 
128             case Favorite.TARGET_EMAIL:
129                 return R.xml.app_target_email;
130 
131             case Favorite.TARGET_BROWSER:
132                 return R.xml.app_target_browser;
133 
134             case Favorite.TARGET_GALLERY:
135                 return R.xml.app_target_gallery;
136 
137             case Favorite.TARGET_CAMERA:
138                 return R.xml.app_target_camera;
139 
140             default:
141                 return 0;
142         }
143     }
144 
encodeItemTypeToFlag(int itemType)145     public static int encodeItemTypeToFlag(int itemType) {
146         return itemType << RESTORE_FLAG_BIT_SHIFT;
147     }
148 
decodeItemTypeFromFlag(int flag)149     public static int decodeItemTypeFromFlag(int flag) {
150         return (flag & ShortcutInfo.FLAG_RESTORED_APP_TYPE) >> RESTORE_FLAG_BIT_SHIFT;
151     }
152 
153 }
154