src/com/beem/project/beem/providers/AvatarProvider.java
author Da Risk <da_risk@beem-project.com>
Fri, 10 Aug 2012 05:53:51 +0200
changeset 985 2a2d82478538
parent 963 24474027eb45
permissions -rw-r--r--
Add an exception for my_avatar id in AvatarProvider. This fix a bug when the my_avatar file does not exist it is not created by openFileHelper.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
     1
/*
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
     2
    BEEM is a videoconference application on the Android Platform.
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
     3
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
     4
    Copyright (C) 2009 by Frederic-Charles Barthelery,
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
     5
                          Jean-Manuel Da Silva,
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
     6
                          Nikita Kozlov,
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
     7
                          Philippe Lago,
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
     8
                          Jean Baptiste Vergely,
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
     9
                          Vincent Veronis.
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    10
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    11
    This file is part of BEEM.
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    12
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    13
    BEEM is free software: you can redistribute it and/or modify
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    14
    it under the terms of the GNU General Public License as published by
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    15
    the Free Software Foundation, either version 3 of the License, or
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    16
    (at your option) any later version.
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    17
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    18
    BEEM is distributed in the hope that it will be useful,
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    19
    but WITHOUT ANY WARRANTY; without even the implied warranty of
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    20
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    21
    GNU General Public License for more details.
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    22
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    23
    You should have received a copy of the GNU General Public License
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    24
    along with BEEM.  If not, see <http://www.gnu.org/licenses/>.
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    25
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    26
    Please send bug reports with examples or suggestions to
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    27
    contact@beem-project.com or http://dev.beem-project.com/
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    28
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    29
    Epitech, hereby disclaims all copyright interest in the program "Beem"
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    30
    written by Frederic-Charles Barthelery,
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    31
               Jean-Manuel Da Silva,
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    32
               Nikita Kozlov,
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    33
               Philippe Lago,
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    34
               Jean Baptiste Vergely,
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    35
               Vincent Veronis.
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    36
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    37
    Nicolas Sadirac, November 26, 2009
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    38
    President of Epitech.
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    39
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    40
    Flavien Astraud, November 26, 2009
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    41
    Head of the EIP Laboratory.
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    42
934
6b30719f4de6 merge otr repo with trunk
Nikita Kozlov <nikita@elyzion.net>
parents: 929
diff changeset
    43
*/
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    44
package com.beem.project.beem.providers;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    45
963
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
    46
import java.io.File;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
    47
import java.io.FileNotFoundException;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
    48
import java.io.IOException;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
    49
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    50
import android.content.ContentProvider;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    51
import android.content.ContentValues;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    52
import android.content.UriMatcher;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    53
import android.database.Cursor;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    54
import android.database.MatrixCursor;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    55
import android.net.Uri;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    56
import android.os.Environment;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    57
import android.os.ParcelFileDescriptor;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    58
import android.util.Log;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    59
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    60
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    61
/**
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    62
 * A simple content provider we expose the differents avatar downloaded.
934
6b30719f4de6 merge otr repo with trunk
Nikita Kozlov <nikita@elyzion.net>
parents: 929
diff changeset
    63
 *
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    64
 */
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    65
public class AvatarProvider extends ContentProvider {
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    66
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    67
    /** The content uri of this provider. */
934
6b30719f4de6 merge otr repo with trunk
Nikita Kozlov <nikita@elyzion.net>
parents: 929
diff changeset
    68
    public static final Uri CONTENT_URI =
6b30719f4de6 merge otr repo with trunk
Nikita Kozlov <nikita@elyzion.net>
parents: 929
diff changeset
    69
	Uri.parse("content://com.beem.project.beem.providers.avatarprovider");
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    70
963
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
    71
    /** The MIME type of a CONTENT_URI directory of Beem avatars.  */
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
    72
    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.com.beem.project.beem.avatar";
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
    73
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
    74
    /** The MIME type of a CONTENT_URI subdirectory of a single Beem avatar.  */
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
    75
    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.com.beem.project.beem.avatar";
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
    76
985
2a2d82478538 Add an exception for my_avatar id in AvatarProvider.
Da Risk <da_risk@beem-project.com>
parents: 963
diff changeset
    77
    /**
2a2d82478538 Add an exception for my_avatar id in AvatarProvider.
Da Risk <da_risk@beem-project.com>
parents: 963
diff changeset
    78
     * Id of the user avatar.
2a2d82478538 Add an exception for my_avatar id in AvatarProvider.
Da Risk <da_risk@beem-project.com>
parents: 963
diff changeset
    79
     */
2a2d82478538 Add an exception for my_avatar id in AvatarProvider.
Da Risk <da_risk@beem-project.com>
parents: 963
diff changeset
    80
    public static final String MY_AVATAR_ID = "my_avatar";
2a2d82478538 Add an exception for my_avatar id in AvatarProvider.
Da Risk <da_risk@beem-project.com>
parents: 963
diff changeset
    81
963
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
    82
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    83
    private static final String TAG = AvatarProvider.class.getSimpleName();
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    84
    private static final String AUTHORITY = "com.beem.project.beem.providers.avatarprovider";
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    85
963
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
    86
    private static String[] columnNames = new String[] {Columns.ID, Columns.DATA};
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    87
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    88
    private static final int AVATAR = 1;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    89
    private static final int AVATAR_ID = 2;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    90
    private static final UriMatcher URIMATCHER = new UriMatcher(AVATAR);
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    91
934
6b30719f4de6 merge otr repo with trunk
Nikita Kozlov <nikita@elyzion.net>
parents: 929
diff changeset
    92
    static
6b30719f4de6 merge otr repo with trunk
Nikita Kozlov <nikita@elyzion.net>
parents: 929
diff changeset
    93
    {
6b30719f4de6 merge otr repo with trunk
Nikita Kozlov <nikita@elyzion.net>
parents: 929
diff changeset
    94
        URIMATCHER.addURI(AUTHORITY, "*", AVATAR_ID);
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    95
	// should not be needed if we pass AVATAR on the constructor but it does not work
934
6b30719f4de6 merge otr repo with trunk
Nikita Kozlov <nikita@elyzion.net>
parents: 929
diff changeset
    96
        URIMATCHER.addURI(AUTHORITY, null, AVATAR);
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    97
    }
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    98
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
    99
    private String mDataPath;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   100
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   101
    /**
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   102
     * Create an AvatarProvider.
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   103
     */
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   104
    public AvatarProvider() {
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   105
    }
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   106
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   107
    @Override
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   108
    public boolean onCreate() {
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   109
	File cacheDir = Environment.getExternalStorageDirectory();
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   110
	File dataPath = new File(cacheDir, "/Android/data/com.beem.project.beem/cache/avatar");
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   111
	dataPath.mkdirs();
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   112
	mDataPath = dataPath.getAbsolutePath();
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   113
	return true;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   114
    }
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   115
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   116
    @Override
934
6b30719f4de6 merge otr repo with trunk
Nikita Kozlov <nikita@elyzion.net>
parents: 929
diff changeset
   117
    public ParcelFileDescriptor openFile(Uri uri, String mode)
6b30719f4de6 merge otr repo with trunk
Nikita Kozlov <nikita@elyzion.net>
parents: 929
diff changeset
   118
	throws FileNotFoundException {
963
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   119
    	return openFileHelper(uri, mode);
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   120
    }
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   121
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   122
    @Override
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   123
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   124
	MatrixCursor c = new MatrixCursor(columnNames);
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   125
	int match = URIMATCHER.match(uri);
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   126
	switch (match) {
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   127
	    case AVATAR:
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   128
		File[] files = new File(mDataPath).listFiles();
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   129
		if (files != null) {
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   130
		    for (File f : files) {
963
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   131
			c.newRow().add(f.getName()).add(f.getAbsolutePath());
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   132
		    }
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   133
		}
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   134
		break;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   135
	    case AVATAR_ID:
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   136
		String id = uri.getPathSegments().get(0);
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   137
		File f = new File(mDataPath, id);
985
2a2d82478538 Add an exception for my_avatar id in AvatarProvider.
Da Risk <da_risk@beem-project.com>
parents: 963
diff changeset
   138
		if (f.exists() || MY_AVATAR_ID.equals(f.getName()))
963
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   139
			c.newRow().add(f.getName()).add(f.getAbsolutePath());
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   140
		break;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   141
	    default:
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   142
		Log.w(TAG, "Unsupported uri for query match = " + match);
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   143
	}
963
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   144
	if (c != null)
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   145
		c.setNotificationUri(getContext().getContentResolver(), uri);
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   146
	return c;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   147
    }
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   148
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   149
    @Override
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   150
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
963
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   151
	int match = URIMATCHER.match(uri);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   152
	String id = null;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   153
	switch (match) {
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   154
	    case AVATAR_ID:
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   155
		id = uri.getPathSegments().get(0);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   156
		break;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   157
	    default:
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   158
		Log.w(TAG, "Unsupported uri for query match = " + match);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   159
	}
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   160
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   161
	if (id == null)
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   162
	    return 0;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   163
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   164
	File f = new File(mDataPath, id);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   165
	try {
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   166
	    f.createNewFile();
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   167
	    getContext().getContentResolver().notifyChange(uri, null);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   168
	    return 1;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   169
	} catch (IOException e) {
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   170
	    Log.e(TAG, "Error while creating file", e);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   171
	}
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   172
	return 0;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   173
    }
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   174
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   175
    @Override
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   176
    public int delete(Uri uri, String selection, String[] selectionArgs) {
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   177
	int res = 0;
963
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   178
	boolean all = false;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   179
	String id = null;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   180
	int match = URIMATCHER.match(uri);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   181
	switch (match) {
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   182
	    case AVATAR_ID:
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   183
		id = uri.getPathSegments().get(0);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   184
		break;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   185
	    case AVATAR:
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   186
		all = true;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   187
		break;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   188
	    default:
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   189
		Log.w(TAG, "Unsupported uri for query match = " + match);
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   190
	}
963
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   191
	File[] list = null;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   192
	if (id != null) {
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   193
	    list = new File[] {new File(mDataPath, id) };
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   194
	} else if (all) {
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   195
	    list = new File(mDataPath).listFiles();
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   196
	}
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   197
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   198
	if (list == null)
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   199
	    return res;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   200
	for (File data : list) {
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   201
	    if (data.exists() && data.delete())
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   202
		res++;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   203
	}
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   204
	if (res > 0)
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   205
	    getContext().getContentResolver().notifyChange(uri, null);
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   206
	return res;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   207
    }
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   208
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   209
    @Override
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   210
    public Uri insert(Uri uri, ContentValues values) {
963
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   211
	int match = URIMATCHER.match(uri);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   212
	String id = null;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   213
	Uri result = null;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   214
	switch (match) {
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   215
	    case AVATAR:
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   216
		id = values.getAsString(Columns.ID);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   217
		result = Uri.withAppendedPath(uri, id);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   218
		break;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   219
	    case AVATAR_ID:
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   220
		id = uri.getPathSegments().get(0);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   221
		result = uri;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   222
		break;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   223
	    default:
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   224
		Log.w(TAG, "Unsupported uri for query match = " + match);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   225
	}
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   226
	if (id == null)
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   227
	    return null;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   228
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   229
	File f = new File(mDataPath, id);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   230
	try {
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   231
	    f.createNewFile();
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   232
	    if (result != null)
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   233
		getContext().getContentResolver().notifyChange(result, null);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   234
	    return result;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   235
	} catch (IOException e) {
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   236
	    Log.e(TAG, "Error while creating file", e);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   237
	}
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   238
	return null;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   239
    }
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   240
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   241
    @Override
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   242
    public String getType(Uri uri) {
963
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   243
    	int match = URIMATCHER.match(uri);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   244
    	switch (match) {
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   245
    	    case AVATAR:
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   246
    		return CONTENT_TYPE;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   247
    	    case AVATAR_ID:
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   248
    		return CONTENT_ITEM_TYPE;
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   249
    	    default:
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   250
    		Log.w(TAG, "Unsupported uri for query match = " + match);
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   251
    	}
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   252
	return null;
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   253
    }
963
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   254
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   255
    /**
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   256
     * The differents columns available in the AvatarProvider.
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   257
     */
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   258
    public interface Columns {
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   259
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   260
	/** The id of the avatar.
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   261
	 * type: string */
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   262
	String ID = "_id";
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   263
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   264
	/** The path of the avatar file.
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   265
	 * type: string
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   266
	 * This field is readonly */
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   267
	String DATA = "_data";
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   268
    }
24474027eb45 Complete the avatar provider to support insert update and delete.
Da Risk <da_risk@beem-project.com>
parents: 934
diff changeset
   269
865
70ca3ab6e459 Use a content provider to store the avatars
Da Risk <darisk972@gmail.com>
parents:
diff changeset
   270
}