Added Application Suggestions.

Added in custom Resolver to handle providing suggestions.

Added in Service to handle providing suggestions to custom resolver.

Added in ability to provider suggestions through a Proxy to another
application which must be installed during compile time if one is
to be used. This is a similar implementation to how the Location
Services work.

Change-Id: Id960260596b7bb6485caa1e1d07744e387a4c6e9
This commit is contained in:
herriojr
2015-09-08 13:59:20 -07:00
parent 35df988aa5
commit e78ca4d6fe
27 changed files with 2274 additions and 0 deletions

View File

@@ -85,4 +85,9 @@ public final class CMContextConstants {
* @hide
*/
public static final String CM_HARDWARE_SERVICE = "cmhardware";
/**
* @hide
*/
public static final String CM_APP_SUGGEST_SERVICE = "cmappsuggest";
}

View File

@@ -0,0 +1,140 @@
/**
* Copyright (c) 2015, The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cyanogenmod.app.suggest;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import cyanogenmod.app.CMContextConstants;
import cyanogenmod.app.suggest.ApplicationSuggestion;
/**
* Provides an interface to get information about suggested apps for an intent which may include
* applications not installed on the device. This is used by the CMResolver in order to provide
* suggestions when an intent is fired but no application exists for the given intent.
*
* @hide
*/
public class AppSuggestManager {
private static final String TAG = AppSuggestManager.class.getSimpleName();
private static final boolean DEBUG = true;
private static IAppSuggestManager sImpl;
private static AppSuggestManager sInstance;
private Context mContext;
/**
* Gets an instance of the AppSuggestManager.
*
* @param context
*
* @return An instance of the AppSuggestManager
*/
public static synchronized AppSuggestManager getInstance(Context context) {
if (sInstance != null) {
return sInstance;
}
context = context.getApplicationContext() != null ? context.getApplicationContext() : context;
sInstance = new AppSuggestManager(context);
return sInstance;
}
private AppSuggestManager(Context context) {
mContext = context.getApplicationContext();
}
private static synchronized IAppSuggestManager getService() {
if (sImpl == null) {
IBinder b = ServiceManager.getService(CMContextConstants.CM_APP_SUGGEST_SERVICE);
if (b != null) {
sImpl = IAppSuggestManager.Stub.asInterface(b);
} else {
Log.e(TAG, "Unable to find implementation for app suggest service");
}
}
return sImpl;
}
/**
* Checks to see if an intent is handled by the App Suggestions Service. This should be
* implemented in such a way that it is safe to call inline on the UI Thread.
*
* @param intent The intent
* @return true if the App Suggestions Service has suggestions for this intent, false otherwise
*/
public boolean handles(Intent intent) {
IAppSuggestManager mgr = getService();
if (mgr == null) return false;
try {
return mgr.handles(intent);
} catch (RemoteException e) {
return false;
}
}
/**
*
* Gets a list of the suggestions for the given intent.
*
* @param intent The intent
* @return A list of application suggestions or an empty list if none.
*/
public List<ApplicationSuggestion> getSuggestions(Intent intent) {
IAppSuggestManager mgr = getService();
if (mgr == null) return new ArrayList<>(0);
try {
return mgr.getSuggestions(intent);
} catch (RemoteException e) {
return new ArrayList<>(0);
}
}
/**
* Loads the icon for the given suggestion.
*
* @param suggestion The suggestion to load the icon for
*
* @return A {@link Drawable} or null if one cannot be found
*/
public Drawable loadIcon(ApplicationSuggestion suggestion) {
try {
InputStream is = mContext.getContentResolver()
.openInputStream(suggestion.getThumbailUri());
return Drawable.createFromStream(is, null);
} catch (FileNotFoundException e) {
return null;
}
}
}

View File

@@ -0,0 +1,22 @@
/**
* Copyright (c) 2015, The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cyanogenmod.app.suggest;
/**
* @hide
*/
parcelable ApplicationSuggestion;

View File

@@ -0,0 +1,118 @@
/**
* Copyright (c) 2015, The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cyanogenmod.app.suggest;
import android.annotation.NonNull;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import cyanogenmod.os.Build;
/**
* @hide
*/
public class ApplicationSuggestion implements Parcelable {
public static final Creator<ApplicationSuggestion> CREATOR =
new Creator<ApplicationSuggestion>() {
public ApplicationSuggestion createFromParcel(Parcel in) {
return new ApplicationSuggestion(in);
}
public ApplicationSuggestion[] newArray(int size) {
return new ApplicationSuggestion[size];
}
};
private String mName;
private String mPackage;
private Uri mDownloadUri;
private Uri mThumbnailUri;
public ApplicationSuggestion(@NonNull String name, @NonNull String pkg,
@NonNull Uri downloadUri, @NonNull Uri thumbnailUri) {
mName = name;
mPackage = pkg;
mDownloadUri = downloadUri;
mThumbnailUri = thumbnailUri;
}
private ApplicationSuggestion(Parcel in) {
// Read parcelable version, make sure to define explicit changes
// within {@link Build.PARCELABLE_VERSION);
int parcelableVersion = in.readInt();
int parcelableSize = in.readInt();
int startPosition = in.dataPosition();
if (parcelableVersion >= Build.CM_VERSION_CODES.APRICOT) {
mName = in.readString();
mPackage = in.readString();
mDownloadUri = in.readParcelable(Uri.class.getClassLoader());
mThumbnailUri = in.readParcelable(Uri.class.getClassLoader());
}
in.setDataPosition(startPosition + parcelableSize);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
// Write parcelable version, make sure to define explicit changes
// within {@link Build.PARCELABLE_VERSION);
out.writeInt(Build.PARCELABLE_VERSION);
// Inject a placeholder that will store the parcel size from this point on
// (not including the size itself).
int sizePosition = out.dataPosition();
out.writeInt(0);
int startPosition = out.dataPosition();
out.writeString(mName);
out.writeString(mPackage);
out.writeParcelable(mDownloadUri, flags);
out.writeParcelable(mThumbnailUri, flags);
// Go back and write size
int parcelableSize = out.dataPosition() - startPosition;
out.setDataPosition(sizePosition);
out.writeInt(parcelableSize);
out.setDataPosition(startPosition + parcelableSize);
}
public String getName() {
return mName;
}
public String getPackageName() {
return mPackage;
}
public Uri getDownloadUri() {
return mDownloadUri;
}
public Uri getThumbailUri() {
return mThumbnailUri;
}
}

View File

@@ -0,0 +1,30 @@
/**
* Copyright (c) 2015, The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cyanogenmod.app.suggest;
import android.content.Intent;
import cyanogenmod.app.suggest.ApplicationSuggestion;
/**
* @hide
*/
interface IAppSuggestManager {
boolean handles(in Intent intent);
List<ApplicationSuggestion> getSuggestions(in Intent intent);
}

View File

@@ -0,0 +1,30 @@
/**
* Copyright (c) 2015, The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cyanogenmod.app.suggest;
import android.content.Intent;
import cyanogenmod.app.suggest.ApplicationSuggestion;
/**
* @hide
*/
interface IAppSuggestProvider {
boolean handles(in Intent intent);
List<ApplicationSuggestion> getSuggestions(in Intent intent);
}