1 /*
2  * Copyright (C) 2016 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.tv.util;
18 
19 import android.app.SearchManager;
20 import android.content.UriMatcher;
21 import android.media.tv.TvContract;
22 import android.net.Uri;
23 import android.support.annotation.IntDef;
24 import com.android.tv.search.LocalSearchProvider;
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 
28 /** Utility class to aid in matching URIs in TvProvider. */
29 public class TvUriMatcher {
30     private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
31 
32     @Retention(RetentionPolicy.SOURCE)
33     @IntDef({
34         MATCH_CHANNEL,
35         MATCH_CHANNEL_ID,
36         MATCH_PROGRAM,
37         MATCH_PROGRAM_ID,
38         MATCH_RECORDED_PROGRAM,
39         MATCH_RECORDED_PROGRAM_ID,
40         MATCH_WATCHED_PROGRAM_ID,
41         MATCH_ON_DEVICE_SEARCH
42     })
43     private @interface TvProviderUriMatchCode {}
44     /** The code for the channels URI. */
45     public static final int MATCH_CHANNEL = 1;
46     /** The code for the channel URI. */
47     public static final int MATCH_CHANNEL_ID = 2;
48     /** The code for the programs URI. */
49     public static final int MATCH_PROGRAM = 3;
50     /** The code for the program URI. */
51     public static final int MATCH_PROGRAM_ID = 4;
52     /** The code for the recorded programs URI. */
53     public static final int MATCH_RECORDED_PROGRAM = 5;
54     /** The code for the recorded program URI. */
55     public static final int MATCH_RECORDED_PROGRAM_ID = 6;
56     /** The code for the watched program URI. */
57     public static final int MATCH_WATCHED_PROGRAM_ID = 7;
58     /** The code for the on-device search URI. */
59     public static final int MATCH_ON_DEVICE_SEARCH = 8;
60 
61     static {
URI_MATCHER.addURI(TvContract.AUTHORITY, "channel", MATCH_CHANNEL)62         URI_MATCHER.addURI(TvContract.AUTHORITY, "channel", MATCH_CHANNEL);
URI_MATCHER.addURI(TvContract.AUTHORITY, "channel/#", MATCH_CHANNEL_ID)63         URI_MATCHER.addURI(TvContract.AUTHORITY, "channel/#", MATCH_CHANNEL_ID);
URI_MATCHER.addURI(TvContract.AUTHORITY, "program", MATCH_PROGRAM)64         URI_MATCHER.addURI(TvContract.AUTHORITY, "program", MATCH_PROGRAM);
URI_MATCHER.addURI(TvContract.AUTHORITY, "program/#", MATCH_PROGRAM_ID)65         URI_MATCHER.addURI(TvContract.AUTHORITY, "program/#", MATCH_PROGRAM_ID);
URI_MATCHER.addURI(TvContract.AUTHORITY, "recorded_program", MATCH_RECORDED_PROGRAM)66         URI_MATCHER.addURI(TvContract.AUTHORITY, "recorded_program", MATCH_RECORDED_PROGRAM);
URI_MATCHER.addURI(TvContract.AUTHORITY, "recorded_program/#", MATCH_RECORDED_PROGRAM_ID)67         URI_MATCHER.addURI(TvContract.AUTHORITY, "recorded_program/#", MATCH_RECORDED_PROGRAM_ID);
URI_MATCHER.addURI(TvContract.AUTHORITY, "watched_program/#", MATCH_WATCHED_PROGRAM_ID)68         URI_MATCHER.addURI(TvContract.AUTHORITY, "watched_program/#", MATCH_WATCHED_PROGRAM_ID);
URI_MATCHER.addURI( LocalSearchProvider.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", MATCH_ON_DEVICE_SEARCH)69         URI_MATCHER.addURI(
70                 LocalSearchProvider.AUTHORITY,
71                 SearchManager.SUGGEST_URI_PATH_QUERY + "/*",
72                 MATCH_ON_DEVICE_SEARCH);
73     }
74 
TvUriMatcher()75     private TvUriMatcher() {}
76 
77     /**
78      * Try to match against the path in a url.
79      *
80      * @see UriMatcher#match
81      */
82     @SuppressWarnings("WrongConstant")
83     @TvProviderUriMatchCode
match(Uri uri)84     public static int match(Uri uri) {
85         return URI_MATCHER.match(uri);
86     }
87 }
88