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);
    }
  }
Example #2
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");
  }