Home | History | Annotate | Download | only in photoviewersample
      1 package com.example.photoviewersample;
      2 
      3 import android.content.ContentProvider;
      4 import android.content.ContentValues;
      5 import android.content.UriMatcher;
      6 import android.content.res.AssetFileDescriptor;
      7 import android.database.Cursor;
      8 import android.database.MatrixCursor;
      9 import android.net.Uri;
     10 
     11 import java.io.FileNotFoundException;
     12 import java.io.IOException;
     13 import java.util.List;
     14 
     15 public class SampleProvider extends ContentProvider {
     16     private static final int PHOTOS = 1;
     17     private static final int PHOTO_INDIVIDUAL_1 = 2;
     18     private static final int PHOTO_INDIVIDUAL_2 = 3;
     19     private static final int PHOTO_INDIVIDUAL_3 = 4;
     20     private static final int PHOTO_INDIVIDUAL_4 = 5;
     21 
     22     private static final String PROVIDER_URI = "com.example.photoviewersample.SampleProvider";
     23 
     24  // Creates a UriMatcher object.
     25     private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
     26 
     27     static
     28     {
     29         sUriMatcher.addURI(PROVIDER_URI, "photos", PHOTOS);
     30         sUriMatcher.addURI(PROVIDER_URI, "photos/1", PHOTO_INDIVIDUAL_1);
     31         sUriMatcher.addURI(PROVIDER_URI, "photos/2", PHOTO_INDIVIDUAL_2);
     32         sUriMatcher.addURI(PROVIDER_URI, "photos/3", PHOTO_INDIVIDUAL_3);
     33         sUriMatcher.addURI(PROVIDER_URI, "photos/4", PHOTO_INDIVIDUAL_4);
     34     }
     35 
     36     @Override
     37     public int delete(Uri uri, String selection, String[] selectionArgs) {
     38         // TODO Auto-generated method stub
     39         return 0;
     40     }
     41 
     42     @Override
     43     public String getType(Uri uri) {
     44         // TODO Auto-generated method stub
     45         return null;
     46     }
     47 
     48     @Override
     49     public Uri insert(Uri uri, ContentValues values) {
     50         // TODO Auto-generated method stub
     51         return null;
     52     }
     53 
     54     @Override
     55     public boolean onCreate() {
     56         // TODO Auto-generated method stub
     57         return false;
     58     }
     59 
     60     @Override
     61     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
     62             String sortOrder) {
     63         MatrixCursor matrix = new MatrixCursor(projection);
     64 
     65         /*
     66          * Choose the table to query and a sort order based on the code returned for the incoming
     67          * URI. Here, too, only the statements for table 3 are shown.
     68          */
     69         switch (sUriMatcher.match(uri)) {
     70             // If the incoming URI was for all of the photos table
     71             case PHOTOS:
     72                 addRow(matrix, PHOTO_INDIVIDUAL_1);
     73                 addRow(matrix, PHOTO_INDIVIDUAL_2);
     74                 addRow(matrix, PHOTO_INDIVIDUAL_3);
     75                 addRow(matrix, PHOTO_INDIVIDUAL_4);
     76                 break;
     77 
     78             // If the incoming URI was for a single row
     79             case PHOTO_INDIVIDUAL_1:
     80                 addRow(matrix, PHOTO_INDIVIDUAL_1);
     81                 break;
     82             case PHOTO_INDIVIDUAL_2:
     83                 addRow(matrix, PHOTO_INDIVIDUAL_2);
     84                 break;
     85             case PHOTO_INDIVIDUAL_3:
     86                 addRow(matrix, PHOTO_INDIVIDUAL_3);
     87                 break;
     88             case PHOTO_INDIVIDUAL_4:
     89                 addRow(matrix, PHOTO_INDIVIDUAL_4);
     90                 break;
     91 
     92             default:
     93                 // If the URI is not recognized, you should do some error handling here.
     94         }
     95         // call the code to actually do the query
     96 
     97         return matrix;
     98     }
     99 
    100     /**
    101      * Adds a single row to the Cursor. A real implementation should
    102      * check the projection to properly match the columns.
    103      */
    104     private void addRow(MatrixCursor matrix, int match_id) {
    105         switch (match_id) {
    106             case PHOTO_INDIVIDUAL_1:
    107                 matrix.newRow()
    108                 .add("content://" + PROVIDER_URI + "/photos/1")                 // uri
    109                 .add("blah.png")                                                // displayName
    110                 .add("content://" + PROVIDER_URI + "/photos/1/contentUri")      // contentUri
    111                 .add("content://" + PROVIDER_URI + "/photos/1/thumbnailUri")    // thumbnailUri
    112                 .add("image/png");                                              // contentType
    113                 break;
    114             case PHOTO_INDIVIDUAL_2:
    115                 matrix.newRow()
    116                 .add("content://" + PROVIDER_URI + "/photos/2")                 // uri
    117                 .add("johannson.png")                                           // displayName
    118                 .add("content://" + PROVIDER_URI + "/photos/2/contentUri")      // contentUri
    119                 .add("content://" + PROVIDER_URI + "/photos/2/thumbnailUri")    // thumbnailUri
    120                 .add("image/png");                                              // contentType
    121                 break;
    122             case PHOTO_INDIVIDUAL_3:
    123                 matrix.newRow()
    124                 .add("content://" + PROVIDER_URI + "/photos/3")                 // uri
    125                 .add("planets.png")                                             // displayName
    126                 .add("content://" + PROVIDER_URI + "/photos/3/contentUri")      // contentUri
    127                 .add("content://" + PROVIDER_URI + "/photos/3/thumbnailUri")    // thumbnailUri
    128                 .add("image/png");                                              // contentType
    129                 break;
    130             case PHOTO_INDIVIDUAL_4:
    131                 matrix.newRow()
    132                 .add("content://" + PROVIDER_URI + "/photos/4")                 // uri
    133                 .add("galaxy.png")                                              // displayName
    134                 .add("content://" + PROVIDER_URI + "/photos/4/contentUri")      // contentUri
    135                 .add("content://" + PROVIDER_URI + "/photos/4/thumbnailUri")    // thumbnailUri
    136                 .add("image/png");                                              // contentType
    137                 break;
    138 
    139             default:
    140                 // If the URI is not recognized, you should do some error handling here.
    141         }
    142     }
    143 
    144     @Override
    145     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    146         // TODO Auto-generated method stub
    147         return 0;
    148     }
    149 
    150     @Override
    151     public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
    152         List<String> pathSegments = uri.getPathSegments();
    153         final int id = Integer.parseInt(pathSegments.get(1));
    154         String fileName;
    155         switch (id) {
    156             case 1:
    157                 fileName = "blah.png";
    158                 break;
    159             case 2:
    160                 fileName = "johannson.png";
    161                 break;
    162             case 3:
    163                 fileName = "planets.png";
    164                 break;
    165             case 4:
    166                 fileName = "galaxy.png";
    167                 break;
    168             default:
    169                 fileName = null;
    170                 break;
    171         }
    172         try {
    173             return getContext().getAssets().openFd(fileName);
    174         } catch (IOException e) {
    175             e.printStackTrace();
    176             return null;
    177         }
    178     }
    179 }
    180