Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 746b7910 authored by jruesga's avatar jruesga
Browse files

Revert Association Provider. Instead, use AOSP PreferredActivity api.

parent 54e31dcf
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -48,11 +48,6 @@
      android:authorities="com.cyanogenmod.explorer.providers.bookmarks"
      android:exported="false" />

    <provider
      android:name=".providers.AssociationsContentProvider"
      android:authorities="com.cyanogenmod.explorer.providers.associations"
      android:exported="false" />

    <activity
      android:name=".activities.NavigationActivity"
      android:label="@string/app_name"
+0 −258
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 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 com.cyanogenmod.explorer.model;

import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.BaseColumns;

import com.cyanogenmod.explorer.providers.AssociationsContentProvider;

import java.io.Serializable;

/**
 * A class that represent a association.
 */
public class Association implements Serializable, Comparable<Association>, Parcelable {

    /**
     * Enumeration for types of associations.
     */
    public enum ASSOCIATION_TYPE {
        /**
         * Associations for open with command.<br/>
         * The ref field contains a file extension
         */
        OPENWITH
    }

    private static final long serialVersionUID = 4742766995763317692L;

    /**
     * Columns of the database
     */
    public static class Columns implements BaseColumns {
        /**
         * The content:// style URL for this table
         */
        public static final Uri CONTENT_URI =
            Uri.parse(
                String.format(
                    "%s%s/%s", //$NON-NLS-1$
                    "content://", //$NON-NLS-1$
                    AssociationsContentProvider.AUTHORITY,
                     "/associations")); //$NON-NLS-1$

        /**
         * The type of the association.
         * <P>Type: TEXT</P>
         * @see ASSOCIATION_TYPE
         */
        public static final String TYPE = "type"; //$NON-NLS-1$

        /**
         * The reference of the association (a file extensions, an intent, ...)
         * <P>Type: TEXT</P>
         */
        public static final String REF = "ref"; //$NON-NLS-1$

        /**
         * The action of an intent of the association
         * <P>Type: TEXT</P>
         */
        public static final String INTENT = "intent"; //$NON-NLS-1$

        /**
         * The default sort order for this table
         */
        public static final String DEFAULT_SORT_ORDER =
                TYPE + " ASC, " + REF + " ASC"; //$NON-NLS-1$ //$NON-NLS-2$

        /**
         * @hide
         */
        public static final String[] ASSOCIATION_QUERY_COLUMNS = {_ID, TYPE, REF, INTENT};

        /**
         * These save calls to cursor.getColumnIndexOrThrow()
         * THEY MUST BE KEPT IN SYNC WITH ABOVE QUERY COLUMNS
         */
        /**
         * @hide
         */
        public static final int ASSOCIATION_ID_INDEX = 0;
        /**
         * @hide
         */
        public static final int ASSOCIATION_TYPE_INDEX = 1;
        /**
         * @hide
         */
        public static final int ASSOCIATION_REF_INDEX = 2;
        /**
         * @hide
         */
        public static final int ASSOCIATION_INTENT_INDEX = 3;
    }

    /** @hide **/
    public int mId;
    /** @hide **/
    public ASSOCIATION_TYPE mType;
    /** @hide **/
    public String mRef;
    /** @hide **/
    public String mIntent;

    /**
     * Constructor of <code>Association</code>.
     *
     * @param id The id of the association
     * @param type The type of the association
     * @param ref The reference of the association. See {@link ASSOCIATION_TYPE} for a valid
     * reference definition
     * @param intent The action of an intent of the association
     * @hide
     */
    Association(ASSOCIATION_TYPE type, String ref, String intent) {
        super();
        this.mType = type;
        this.mRef = ref;
        this.mIntent = intent;
    }

    /**
     * Constructor of <code>Association</code>.
     *
     * @param c The cursor with the information of the association
     */
    public Association(Cursor c) {
        super();
        this.mId = c.getInt(Columns.ASSOCIATION_ID_INDEX);
        this.mType = ASSOCIATION_TYPE.valueOf(c.getString(Columns.ASSOCIATION_TYPE_INDEX));
        this.mRef = c.getString(Columns.ASSOCIATION_REF_INDEX);
        this.mIntent = c.getString(Columns.ASSOCIATION_INTENT_INDEX);
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + this.mId;
        result = prime * result + ((this.mIntent == null) ? 0 : this.mIntent.hashCode());
        result = prime * result + ((this.mRef == null) ? 0 : this.mRef.hashCode());
        result = prime * result + ((this.mType == null) ? 0 : this.mType.hashCode());
        return result;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Association other = (Association) obj;
        if (this.mId != other.mId)
            return false;
        if (this.mIntent == null) {
            if (other.mIntent != null)
                return false;
        } else if (!this.mIntent.equals(other.mIntent))
            return false;
        if (this.mRef == null) {
            if (other.mRef != null)
                return false;
        } else if (!this.mRef.equals(other.mRef))
            return false;
        if (this.mType != other.mType)
            return false;
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int describeContents() {
        return 0;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.mId);
        dest.writeString(this.mType.toString());
        dest.writeString(this.mRef);
        dest.writeString(this.mIntent);
    }

    /**
     * Method that lets create a deserializer of <code>Association</code>.
     */
    public static final Parcelable.Creator<Association> CREATOR = new Parcelable.Creator<Association>() {
        @Override
        public Association createFromParcel(Parcel in) {
            int id = in.readInt();
            ASSOCIATION_TYPE type = ASSOCIATION_TYPE.valueOf(in.readString());
            String ref = in.readString();
            String intent = in.readString();
            Association a = new Association(type, ref, intent);
            a.mId = id;
            return a;
        }

        @Override
        public Association[] newArray(int size) {
            return new Association[size];
        }
    };

    /**
     * {@inheritDoc}
     */
    @Override
    public int compareTo(Association another) {
        int c = this.mType.compareTo(another.mType);
        if (c != 0) {
            return c;
        }
        return this.mRef.compareTo(another.mRef);
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Association [id=" + this.mId + ", type=" + //$NON-NLS-1$ //$NON-NLS-2$
                this.mType + ", ref=" + this.mRef + ", intent=" + //$NON-NLS-1$ //$NON-NLS-2$
                this.mIntent + "]"; //$NON-NLS-1$
    }

}
+0 −174
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 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 com.cyanogenmod.explorer.preferences;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.cyanogenmod.explorer.model.Association;
import com.cyanogenmod.explorer.model.Association.ASSOCIATION_TYPE;

/**
 * A class for deal with user-defined associations
 */
public class Associations {

    private final static int INVALID_ASSOCIATIONS_ID = -1;

    /**
     * Method that add a new association
     *
     * @param context The current context
     * @param association The association to add or update
     * @return Association The association added
     */
    public static Association addAssociation(Context context, Association association) {
        // Check that has a valid information
        if (association.mType == null || association.mRef == null || association.mIntent == null) {
            return null;
        }

        // Retrieve the content resolver
        ContentResolver contentResolver = context.getContentResolver();

        // Check that the associations not exists
        Association b = getAssociationByTypeAndRef(
                            contentResolver, association.mType, association.mRef);
        if (b != null) return b;

        // Create the content values
        ContentValues values = createContentValues(association);
        Uri uri = context.getContentResolver().insert(Association.Columns.CONTENT_URI, values);
        association.mId = (int) ContentUris.parseId(uri);
        if (association.mId == INVALID_ASSOCIATIONS_ID) {
            return null;
        }
        return association;
    }

    /**
     * Method that removes a association
     *
     * @param context The current context
     * @param association The association to delete
     * @return boolean If the associations was remove
     */
    public static boolean removeAssociation(Context context, Association association) {
        // Check that has a valid information
        if (association.mId == INVALID_ASSOCIATIONS_ID) return false;

        // Retrieve the content resolver
        ContentResolver contentResolver = context.getContentResolver();
        Uri uri = ContentUris.withAppendedId(Association.Columns.CONTENT_URI, association.mId);
        return contentResolver.delete(uri, "", null) == 1; //$NON-NLS-1$
    }

    /**
     * Method that return the association from his identifier
     *
     * @param contentResolver The content resolver
     * @param associationId The association identifier
     * @return Association The association. null if no association exists.
     */
    public static Association getAssociation(ContentResolver contentResolver, int associationId) {
        Cursor cursor = contentResolver.query(
                ContentUris.withAppendedId(Association.Columns.CONTENT_URI, associationId),
                Association.Columns.ASSOCIATION_QUERY_COLUMNS,
                null, null, null);
        Association association = null;
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                association = new Association(cursor);
            }
            cursor.close();
        }
        return association;
    }

    /**
     * Method that return all the associations in the database
     *
     * @param contentResolver The content resolver
     * @return Cursor The association cursor
     */
    public static Cursor getAllAssociations(ContentResolver contentResolver) {
        return contentResolver.query(
                Association.Columns.CONTENT_URI,
                Association.Columns.ASSOCIATION_QUERY_COLUMNS,
                null, null, null);
    }

    /**
     * Method that return all the associations in the database for a type of association
     *
     * @param contentResolver The content resolver
     * @param type The association type
     * @return Cursor The association cursor
     */
    public static Cursor getAllAssociationByType(
            ContentResolver contentResolver, ASSOCIATION_TYPE type) {
        final String where = Association.Columns.TYPE + " = ?"; //$NON-NLS-1$
        return contentResolver.query(
                Association.Columns.CONTENT_URI,
                Association.Columns.ASSOCIATION_QUERY_COLUMNS,
                where, new String[]{type.toString()}, null);
    }

    /**
     * Method that return the association from his ref and type
     *
     * @param contentResolver The content resolver
     * @param type The association type
     * @param ref The association identifier
     * @return Association The association. null if no association exists.
     */
    public static Association getAssociationByTypeAndRef(
            ContentResolver contentResolver, ASSOCIATION_TYPE type, String ref) {
        final String where = Association.Columns.TYPE + " = ? and " + //$NON-NLS-1$
                             Association.Columns.REF + " = ?"; //$NON-NLS-1$
        Cursor cursor = contentResolver.query(
                Association.Columns.CONTENT_URI,
                Association.Columns.ASSOCIATION_QUERY_COLUMNS,
                where, new String[]{type.toString(), ref}, null);
        Association association = null;
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                association = new Association(cursor);
            }
            cursor.close();
        }
        return association;
    }

    /**
     * Method that create the {@link ContentValues} from the association
     *
     * @param association The association
     * @return ContentValues The content
     */
    private static ContentValues createContentValues(Association association) {
        ContentValues values = new ContentValues(1);
        values.put(Association.Columns.TYPE, association.mType.toString());
        values.put(Association.Columns.REF, association.mRef);
        values.put(Association.Columns.INTENT, association.mIntent);
        return values;
    }
}
+0 −71
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 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 com.cyanogenmod.explorer.preferences;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Helper class for opening the associations database from multiple providers. Also provides
 * some common functionality.
 */
public class AssociationsDatabaseHelper extends SQLiteOpenHelper {

    private static final String TAG = "AssociationsDatabaseHelper"; //$NON-NLS-1$

    private static boolean DEBUG = false;

    private static final String DATABASE_NAME = "associations.db"; //$NON-NLS-1$
    private static final int DATABASE_VERSION = 1;

    /**
     * Constructor of <code>AssociationsDatabaseHelper</code>
     *
     * @param context The current context
     */
    public AssociationsDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE associations (" + //$NON-NLS-1$
                   "_id INTEGER PRIMARY KEY, " + //$NON-NLS-1$
                   "type TEXT, " + //$NON-NLS-1$
                   "ref TEXT, " + //$NON-NLS-1$
                   "intent TEXT);"); //$NON-NLS-1$
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
        if (DEBUG) {
            Log.v(TAG, "Upgrading associations database from version " + //$NON-NLS-1$
                oldVersion + " to " + currentVersion + //$NON-NLS-1$
                ", which will destroy all old data"); //$NON-NLS-1$
        }
        db.execSQL("DROP TABLE IF EXISTS associations"); //$NON-NLS-1$
        onCreate(db);
    }
}
 No newline at end of file
+0 −220
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 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 com.cyanogenmod.explorer.providers;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import com.cyanogenmod.explorer.model.Association;
import com.cyanogenmod.explorer.preferences.AssociationsDatabaseHelper;

/**
 * A content provider for manage the user-defined associations
 */
public class AssociationsContentProvider extends ContentProvider  {

    private static final boolean DEBUG = false;

    private static final String TAG = "AssociationsContentProvider"; //$NON-NLS-1$

    private AssociationsDatabaseHelper mOpenHelper;

    private static final int ASSOCIATIONS = 1;
    private static final int ASSOCIATIONS_ID = 2;

    /**
     * The authority string name.
     */
    public static final String AUTHORITY =
            "com.cyanogenmod.explorer.providers.associations"; //$NON-NLS-1$

    private static final UriMatcher sURLMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        sURLMatcher.addURI(
                AUTHORITY,
                "associations", ASSOCIATIONS); //$NON-NLS-1$
        sURLMatcher.addURI(
                AUTHORITY,
                "associations/#", ASSOCIATIONS_ID); //$NON-NLS-1$
    }

    /**
     * Constructor of <code>AssociationsContentProvider</code>.
     */
    public AssociationsContentProvider() {
        super();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean onCreate() {
        this.mOpenHelper = new AssociationsDatabaseHelper(getContext());
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Cursor query(Uri url, String[] projectionIn, String selection,
            String[] selectionArgs, String sort) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        // Generate the body of the query
        int match = sURLMatcher.match(url);
        switch (match) {
            case ASSOCIATIONS:
                qb.setTables("associations"); //$NON-NLS-1$
                break;
            case ASSOCIATIONS_ID:
                qb.setTables("associations"); //$NON-NLS-1$
                qb.appendWhere("_id="); //$NON-NLS-1$
                qb.appendWhere(url.getPathSegments().get(1));
                break;
            default:
                throw new IllegalArgumentException("Unknown URL " + url); //$NON-NLS-1$
        }

        // Open the database
        SQLiteDatabase db = this.mOpenHelper.getReadableDatabase();
        Cursor cursor = qb.query(db, projectionIn, selection, selectionArgs,
                              null, null, sort);
        if (cursor == null) {
            if (DEBUG) {
                Log.v(TAG, "Associations.query: failed"); //$NON-NLS-1$
            }
        } else {
            cursor.setNotificationUri(getContext().getContentResolver(), url);
        }

        return cursor;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getType(Uri url) {
        int match = sURLMatcher.match(url);
        switch (match) {
            case ASSOCIATIONS:
                return "vnd.android.cursor.dir/associations"; //$NON-NLS-1$
            case ASSOCIATIONS_ID:
                return "vnd.android.cursor.item/associations"; //$NON-NLS-1$
            default:
                throw new IllegalArgumentException("Unknown URL"); //$NON-NLS-1$
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
        int count;
        long rowId = 0;
        int match = sURLMatcher.match(url);
        SQLiteDatabase db = this.mOpenHelper.getWritableDatabase();
        switch (match) {
            case ASSOCIATIONS_ID: {
                String segment = url.getPathSegments().get(1);
                rowId = Long.parseLong(segment);
                count = db.update(
                        "associations", values, "_id=" + rowId, null); //$NON-NLS-1$ //$NON-NLS-2$
                break;
            }
            default: {
                throw new UnsupportedOperationException(
                        "Cannot update URL: " + url); //$NON-NLS-1$
            }
        }
        if (DEBUG) {
            Log.v(TAG,
                  "*** notifyChange() rowId: " + //$NON-NLS-1$
                  rowId + " url " + url); //$NON-NLS-1$
        }
        getContext().getContentResolver().notifyChange(url, null);
        return count;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Uri insert(Uri url, ContentValues initialValues) {
        if (sURLMatcher.match(url) != ASSOCIATIONS) {
            throw new IllegalArgumentException("Cannot insert into URL: " + url); //$NON-NLS-1$
        }

        // Add the association
        SQLiteDatabase db = this.mOpenHelper.getWritableDatabase();
        long rowId = db.insert("associations", null, initialValues); //$NON-NLS-1$
        if (rowId < 0) {
            throw new SQLException("Failed to insert row"); //$NON-NLS-1$
        }
        if (DEBUG) {
            Log.v(TAG, "Added association rowId = " + rowId); //$NON-NLS-1$
        }
        Uri newUrl = ContentUris.withAppendedId(Association.Columns.CONTENT_URI, rowId);

        // Notify changes
        getContext().getContentResolver().notifyChange(newUrl, null);
        return newUrl;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int delete(Uri url, String where, String[] whereArgs) {
        SQLiteDatabase db = this.mOpenHelper.getWritableDatabase();
        int count;
        String whereQuery = where;
        switch (sURLMatcher.match(url)) {
            case ASSOCIATIONS:
                count = db.delete("associations", whereQuery, whereArgs); //$NON-NLS-1$
                break;
            case ASSOCIATIONS_ID:
                String segment = url.getPathSegments().get(1);
                if (TextUtils.isEmpty(whereQuery)) {
                    whereQuery = "_id=" + segment; //$NON-NLS-1$
                } else {
                    whereQuery = "_id=" + segment + //$NON-NLS-1$
                                 " AND (" + whereQuery + ")"; //$NON-NLS-1$ //$NON-NLS-2$
                }
                count = db.delete("associations", whereQuery, whereArgs); //$NON-NLS-1$
                break;
            default:
                throw new IllegalArgumentException("Cannot delete from URL: " + url); //$NON-NLS-1$
        }

        getContext().getContentResolver().notifyChange(url, null);
        return count;
    }
}
 No newline at end of file