简体   繁体   中英

How to access the entire TV Provider database by TV Input Manager?

According to the Android docs

TV Inputs provided and signed by the device manufacturer (signature apps) or other apps installed in the system partition will have access to the entire TV Provider database. This access can be used to construct apps to browse and search across all available TV channels and programs.

Assuming I signed as device manufacturer or installed app in system partition, how can I access the TvProvider and thus its channel information?

EDIT:

val tifSupport: Boolean = packageManager.hasSystemFeature(PackageManager.FEATURE_LIVE_TV)
Log.d("XXX", "TIF Support ? $tifSupport")

This line says true. Then I run these lines:

val tvInputManager = applicationContext.getSystemService(TV_INPUT_SERVICE) as TvInputManager?
Log.d("XXX", "TvInputManager $tvInputManager")

val il = tvInputManager?.tvInputList
Log.d("XXX", "TvInputList size --> ${il?.size}")

tvInputManager?.tvInputList?.forEach { info ->
    Log.d("XXX", "TvInputListInfo ${info.id} ${info.serviceInfo} # ${info.extras.size()}")
}

First log prints

TvInputManager android.media.tv.TvInputManager@95728c2

so the tvInputManager looks valid. Second shows 0 as the TvInputList size and thus third log (in forEach()) is not printed at all.

You have to start with background for Android. By provider in the doc, they mean ContentProvider , which will share information between process within Android. Now to start with:

  • If TV Provider supported by our system.
  • If All the Manifest Permission set to the application
  • If Application installed under the system apps (and has right SE Policy)

Then you will be able to use ContentProvider to fetch all kind of information you need. To see the full support for TVContent Provider you can refer to this file (ensure it's aligned with your Android OS version) and other AOSP information. For ex.

/**
 * Returns the current list of channels your app provides.
 *
 * @param resolver Application's ContentResolver.
 * @return List of channels.
 */
public static List<Channel> getChannels(ContentResolver resolver) {
    List<Channel> channels = new ArrayList<>();
    // TvProvider returns programs in chronological order by default.
    Cursor cursor = null;
    try {
        cursor = resolver.query(Channels.CONTENT_URI, Channel.PROJECTION, null, null, null);
        if (cursor == null || cursor.getCount() == 0) {
            return channels;
        }
        while (cursor.moveToNext()) {
            channels.add(Channel.fromCursor(cursor));
        }
    } catch (Exception e) {
        Log.w(TAG, "Unable to get channels", e);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return channels;
}

Another ex.

TvInputManager tv = (TvInputManager)getApplicationContext().getSystemService(Context.TV_INPUT_SERVICE);

    List<TvInputInfo> list = tv.getTvInputList();
    String[] projection =  {
            TvContract.Channels._ID,
            TvContract.Channels.COLUMN_DISPLAY_NUMBER
    };

    ContentResolver cr = getContentResolver();
    Iterator<TvInputInfo> it = list.iterator();
    while(it.hasNext()) {
        TvInputInfo aux = it.next();
        Uri uri = TvContract.buildChannelsUriForInput(aux.getId());

        Log.d("TAG", uri.toString());
        Log.d("TAG", aux.toString());

        Cursor cur = cr.query(uri, projection, null, null ,null);
        Log.d("TAG", cur.toString());

        if(cur.moveToFirst()) {
            Log.d("TAG", "not empty cursors");
        }

    }

UPDATE:

The basic permission you should have(Also refer to the official documentation ):

<uses-permission android:name="com.android.providers.tv.permission.READ_EPG_DATA" />
<uses-permission android:name="com.android.providers.tv.permission.WRITE_EPG_DATA" />
<uses-permission android:name="com.android.providers.tv.permission.ACCESS_ALL_EPG_DATA"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM