public static void updateChannels(
      Context context, String inputId, List<XmlTvParser.XmlTvChannel> channels) {
    // Create a map from original network ID to channel row ID for existing channels.
    SparseArray<Long> mExistingChannelsMap = new SparseArray<>();
    Uri channelsUri = TvContract.buildChannelsUriForInput(inputId);
    String[] projection = {Channels._ID, Channels.COLUMN_ORIGINAL_NETWORK_ID};
    Cursor cursor = null;
    ContentResolver resolver = context.getContentResolver();
    try {
      cursor = resolver.query(channelsUri, projection, null, null, null);
      while (cursor != null && cursor.moveToNext()) {
        long rowId = cursor.getLong(0);
        int originalNetworkId = cursor.getInt(1);
        mExistingChannelsMap.put(originalNetworkId, rowId);
      }
    } finally {
      if (cursor != null) {
        cursor.close();
      }
    }

    // If a channel exists, update it. If not, insert a new one.
    ContentValues values = new ContentValues();
    values.put(Channels.COLUMN_INPUT_ID, inputId);
    Map<Uri, String> logos = new HashMap<>();
    for (XmlTvParser.XmlTvChannel channel : channels) {
      values.put(Channels.COLUMN_DISPLAY_NUMBER, channel.displayNumber);
      values.put(Channels.COLUMN_DISPLAY_NAME, channel.displayName);
      values.put(Channels.COLUMN_ORIGINAL_NETWORK_ID, channel.originalNetworkId);
      values.put(Channels.COLUMN_TRANSPORT_STREAM_ID, channel.transportStreamId);
      values.put(Channels.COLUMN_SERVICE_ID, channel.serviceId);
      Long rowId = mExistingChannelsMap.get(channel.originalNetworkId);
      Uri uri;
      if (rowId == null) {
        uri = resolver.insert(TvContract.Channels.CONTENT_URI, values);
      } else {
        uri = TvContract.buildChannelUri(rowId);
        resolver.update(uri, values, null, null);
        mExistingChannelsMap.remove(channel.originalNetworkId);
      }
      if (!TextUtils.isEmpty(channel.icon.src)) {
        logos.put(TvContract.buildChannelLogoUri(uri), channel.icon.src);
      }
    }
    if (!logos.isEmpty()) {
      new InsertLogosTask(context).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, logos);
    }

    // Deletes channels which don't exist in the new feed.
    int size = mExistingChannelsMap.size();
    for (int i = 0; i < size; ++i) {
      Long rowId = mExistingChannelsMap.valueAt(i);
      resolver.delete(TvContract.buildChannelUri(rowId), null, null);
    }
  }
  public static LongSparseArray<XmlTvParser.XmlTvChannel> buildChannelMap(
      ContentResolver resolver, String inputId, List<XmlTvParser.XmlTvChannel> channels) {
    Uri uri = TvContract.buildChannelsUriForInput(inputId);
    String[] projection = {TvContract.Channels._ID, TvContract.Channels.COLUMN_DISPLAY_NUMBER};

    LongSparseArray<XmlTvParser.XmlTvChannel> channelMap = new LongSparseArray<>();
    Cursor cursor = null;
    try {
      cursor = resolver.query(uri, projection, null, null, null);
      if (cursor == null || cursor.getCount() == 0) {
        return null;
      }

      while (cursor.moveToNext()) {
        long channelId = cursor.getLong(0);
        String channelNumber = cursor.getString(1);
        channelMap.put(channelId, getChannelByNumber(channelNumber, channels));
      }
    } catch (Exception e) {
      Log.d(TAG, "Content provider query: " + e.getStackTrace());
      return null;
    } finally {
      if (cursor != null) {
        cursor.close();
      }
    }
    return channelMap;
  }
 public static List<PlaybackInfo> getProgramPlaybackInfo(
     ContentResolver resolver,
     Uri channelUri,
     long startTimeMs,
     long endTimeMs,
     int maxProgramInReturn) {
   Uri uri = TvContract.buildProgramsUriForChannel(channelUri, startTimeMs, endTimeMs);
   String[] projection = {
     Programs.COLUMN_START_TIME_UTC_MILLIS,
     Programs.COLUMN_END_TIME_UTC_MILLIS,
     Programs.COLUMN_CONTENT_RATING,
     Programs.COLUMN_INTERNAL_PROVIDER_DATA,
     Programs.COLUMN_CANONICAL_GENRE
   };
   Cursor cursor = null;
   List<PlaybackInfo> list = new ArrayList<>();
   try {
     cursor = resolver.query(uri, projection, null, null, null);
     while (cursor.moveToNext()) {
       long startMs = cursor.getLong(0);
       long endMs = cursor.getLong(1);
       TvContentRating[] ratings = stringToContentRatings(cursor.getString(2));
       Pair<Integer, String> values = parseInternalProviderData(cursor.getString(3));
       int videoType = values.first;
       String videoUrl = values.second;
       list.add(new PlaybackInfo(startMs, endMs, videoUrl, videoType, ratings));
       if (list.size() > maxProgramInReturn) {
         break;
       }
     }
   } catch (Exception e) {
     Log.e(TAG, "Failed to get program playback info from TvProvider.", e);
   } finally {
     if (cursor != null) {
       cursor.close();
     }
   }
   return list;
 }
 public static List<Program> getPrograms(ContentResolver resolver, Uri channelUri) {
   Uri uri = TvContract.buildProgramsUriForChannel(channelUri);
   Cursor cursor = null;
   List<Program> programs = new ArrayList<>();
   try {
     // TvProvider returns programs chronological order by default.
     cursor = resolver.query(uri, null, null, null, null);
     if (cursor == null || cursor.getCount() == 0) {
       return programs;
     }
     while (cursor.moveToNext()) {
       programs.add(Program.fromCursor(cursor));
     }
   } catch (Exception e) {
     Log.w(TAG, "Unable to get programs for " + channelUri, e);
   } finally {
     if (cursor != null) {
       cursor.close();
     }
   }
   return programs;
 }
Ejemplo n.º 5
0
  public void performSync(TvInputProvider provider, String inputId) {
    Log.d(TAG, "Actually begin the sync");
    List<Channel> allChannels = provider.getAllChannels();
    Log.d(TAG, allChannels.toString());
    for (int i = 0; i < allChannels.size(); i++) {
      if (allChannels.get(i).getOriginalNetworkId() == 0)
        allChannels.get(i).setOriginalNetworkId(i + 1);
      if (allChannels.get(i).getTransportStreamId() == 0)
        allChannels.get(i).setTransportStreamId(i + 1);
    }
    TvContractUtils.updateChannels(getContext(), inputId, allChannels);

    LongSparseArray<Channel> channelMap =
        TvContractUtils.buildChannelMap(mContext.getContentResolver(), inputId, allChannels);
    if (channelMap == null) {
      Log.d(TAG, "?");
      Handler h =
          new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
              super.handleMessage(msg);
              Toast.makeText(getContext(), "Couldn't find any channels. Uh-oh.", Toast.LENGTH_SHORT)
                  .show();
            }
          };
      h.sendEmptyMessage(0);
      // Let's not continue running
      return;
    }
    long startMs = new Date().getTime();
    long endMs = startMs + FULL_SYNC_WINDOW_SEC * 1000;
    Log.d(TAG, "Now start to get programs");
    for (int i = 0; i < channelMap.size(); ++i) {
      Uri channelUri = TvContract.buildChannelUri(channelMap.keyAt(i));
      List<Program> programList =
          provider.getProgramsForChannel(channelUri, channelMap.valueAt(i), startMs, endMs);
      Log.d(TAG, "Okay, we NEED to set the channel id first");
      for (Program p : programList) {
        p.setChannelId(channelMap.keyAt(i));
      }
      Log.d(TAG, "For " + channelMap.valueAt(i).toString());
      Log.d(TAG, programList.toString());
      updatePrograms(channelUri, programList);
      // Let's double check programs
      Uri programEditor = TvContract.buildProgramsUriForChannel(channelUri);

      // Mass delete and re-insertion
      /*getContext().getContentResolver().delete(programEditor, null, null);
      for (Program p : programList) {
          p.setChannelId(channelMap.keyAt(i)); //Make sure you have the correct channel id value, it seems to be a foreign key
          Uri insert = getContext().getContentResolver().insert(programEditor, p.toContentValues());
          Log.d(TAG, (insert == null) + " " + p.toString());
          if (insert != null)
              Log.d(TAG, insert.toString());
      }*/

      /*Log.d(TAG, programEditor.toString());
      String[] projection = {TvContract.Programs.COLUMN_TITLE};
      try (Cursor c = getContext().getContentResolver().query(programEditor, projection, null, null, null)) {
          Log.d(TAG, "Found " + c.getCount() + " programs");
          while (c.moveToNext()) {
              Log.d(TAG, "Cursor read " + c.getString(c.getColumnIndex(TvContract.Programs.COLUMN_TITLE)));
          }
      }*/
    }
    Log.d(TAG, "Sync performed");
  }