public ContentProvider addProvider(
     Class<? extends ContentProvider> providerClass, String authority) throws Exception {
   ContentProvider provider = providerClass.newInstance();
   ProviderInfo info = new ProviderInfo();
   info.authority = authority;
   provider.attachInfo(mProviderContext, info);
   resolver.addProvider(authority, provider);
   return provider;
 }
 // NOTE: active commands (query, delete, ..) are called to try to
 // invoke provider code.  Real access happens in the Client code
 // and we need to conntect the Android ContentProviderClient with this.....
 @DSVerified
 @DSBan(DSCat.DROIDSAFE_INTERNAL)
 public static void modelContentProvider(android.content.ContentProvider contentProvider) {
   contentProvider.droidsafeAttachContext(context);
   contentProvider.onCreate();
   contentProvider.onConfigurationChanged(new Configuration());
   contentProvider.onLowMemory();
   contentProvider.onTrimMemory(0);
   // Its not clear if we could figure out some of the value for these parameters
   contentProvider.query(null, null, null, null, null);
   contentProvider.insert(null, null);
   contentProvider.update(null, null, null, null);
   contentProvider.delete(null, null, null);
   contentProvider.getType(null);
 }
  public static int update(
      ContentProvider provider, Uri contentUri, long id, ContentValues values) {

    Uri uri = ContentUris.withAppendedId(contentUri, id);

    return provider.update(uri, values, null, null);
  }
  /** Implementation is provided by the parent class. */
  @Override
  public void attachInfo(Context context, ProviderInfo info) {
    mAuthority = info.authority;

    mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    mMatcher.addURI(mAuthority, "root", MATCH_ROOTS);
    mMatcher.addURI(mAuthority, "root/*", MATCH_ROOT);
    mMatcher.addURI(mAuthority, "root/*/recent", MATCH_RECENT);
    mMatcher.addURI(mAuthority, "root/*/search", MATCH_SEARCH);
    mMatcher.addURI(mAuthority, "document/*", MATCH_DOCUMENT);
    mMatcher.addURI(mAuthority, "document/*/children", MATCH_CHILDREN);

    // Sanity check our setup
    if (!info.exported) {
      throw new SecurityException("Provider must be exported");
    }
    if (!info.grantUriPermissions) {
      throw new SecurityException("Provider must grantUriPermissions");
    }
    if (!android.Manifest.permission.MANAGE_DOCUMENTS.equals(info.readPermission)
        || !android.Manifest.permission.MANAGE_DOCUMENTS.equals(info.writePermission)) {
      throw new SecurityException("Provider must be protected by MANAGE_DOCUMENTS");
    }

    super.attachInfo(context, info);
  }
 @Override
 protected Cursor getCursorForDeletedTable(ContentProvider cp, Class entryClass) {
   if (entryClass != EventEntry.class) {
     throw new IllegalArgumentException("unexpected entry class, " + entryClass.getName());
   }
   return cp.query(Calendar.Events.DELETED_CONTENT_URI, null, null, null, null);
 }
 @Override
 public void handleMessage(Message msg) {
   if (msg.obj instanceof Exception) {
     showErrorToast(getContext(), msg.obj, false);
   }
   super.handleMessage(msg);
 }
 //
 // String
 //
 public static String getString(ContentProvider provider, Uri uri, String key) {
   String value = "";
   Cursor cur = provider.query(uri, null, null, null, null);
   if (cur != null) {
     value = getString(cur, key);
     cur.close();
   }
   return value;
 }
  //
  // Int
  //
  public static int getInt(ContentProvider provider, Uri uri, String key) {
    int value = -1;

    Cursor cur = provider.query(uri, null, null, null, null);
    if (cur != null) {
      value = getInt(cur, key);
      cur.close();
    }
    return value;
  }
 @Override
 public void onTrimMemory(int level) {
   super.onTrimMemory(level);
   if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
     // In the background. Close the database via
     // the helper. If there is an active connection, it will continue to process
     // due to reference counting.
     dbHelper.close();
   }
 }
  public static int update(
      ContentProvider provider, Uri contentUri, long id, String key, long value) {

    Uri uri = ContentUris.withAppendedId(contentUri, id);

    ContentValues values = new ContentValues();

    values.put(key, value);
    return provider.update(uri, values, null, null);
  }
  //
  // Boolean
  //
  public static Boolean getBoolean(ContentProvider provider, Uri uri, String key) {
    Boolean value = false;

    Cursor cur = provider.query(uri, null, null, null, null);
    if (cur != null) {
      value = getBoolean(cur, key);
      cur.close();
    }
    return value;
  }
  /** Implementation is provided by the parent class. */
  @Override
  public void attachInfo(Context context, ProviderInfo info) {
    mAuthority = info.authority;

    mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    mMatcher.addURI(mAuthority, "root", MATCH_ROOTS);
    mMatcher.addURI(mAuthority, "root/*", MATCH_ROOT);
    mMatcher.addURI(mAuthority, "root/*/search", MATCH_SEARCH);
    mMatcher.addURI(mAuthority, "document/*", MATCH_DOCUMENT);
    mMatcher.addURI(mAuthority, "document/*/children", MATCH_CHILDREN);
    mMatcher.addURI(mAuthority, "tree/*/document/*", MATCH_DOCUMENT_TREE);
    mMatcher.addURI(mAuthority, "tree/*/document/*/children", MATCH_CHILDREN_TREE);

    super.attachInfo(context, info);
  }
  //
  // GeoPoint
  //
  public static GeoPoint getPoint(
      ContentProvider provider, Uri uri, String latKeyE6, String longKeyE6) {
    int latE6 = 0;
    int longE6 = 0;

    Cursor cur = provider.query(uri, null, null, null, null);
    if (cur != null) {
      if (cur.moveToNext()) {
        latE6 = cur.getInt(cur.getColumnIndex(latKeyE6));
        longE6 = cur.getInt(cur.getColumnIndex(longKeyE6));
      }
      cur.close();
    }
    return new GeoPoint(latE6, longE6);
  }
 public void attachInfo(Context context, ProviderInfo providerinfo)
 {
     super.attachInfo(context, providerinfo);
     if (providerinfo.exported)
     {
         throw new SecurityException("Provider must not be exported");
     }
     if (!providerinfo.grantUriPermissions)
     {
         throw new SecurityException("Provider must grant uri permissions");
     } else
     {
         mStrategy = getPathStrategy(context, providerinfo.authority);
         return;
     }
 }
  public static int updatePoint(
      ContentProvider provider,
      Uri contentUri,
      long id,
      String latKeyE6,
      String longKeyE6,
      GeoPoint point) {

    Uri uri = ContentUris.withAppendedId(contentUri, id);

    ContentValues values = new ContentValues();

    values.put(latKeyE6, point.getLatitudeE6());
    values.put(longKeyE6, point.getLongitudeE6());
    return provider.update(uri, values, null, null);
  }
 @Override
 @TargetApi(11)
 public void shutdown() {
   mOpenHelper.close();
   super.shutdown();
 }
  public void updateProvider(
      Feed feed, Long syncLocalId, Entry entry, ContentProvider provider, Object info)
      throws ParseException {
    SyncInfo syncInfo = (SyncInfo) info;
    EventEntry event = (EventEntry) entry;

    ContentValues map = new ContentValues();

    // use the calendar's timezone, if provided in the feed.
    // this overwrites whatever was in the db.
    if ((feed != null) && (feed instanceof EventsFeed)) {
      EventsFeed eventsFeed = (EventsFeed) feed;
      syncInfo.calendarTimezone = eventsFeed.getTimezone();
    }

    if (entry.isDeleted()) {
      deletedEntryToContentValues(syncLocalId, event, map);
      if (Config.LOGV) {
        Log.v(TAG, "Deleting entry: " + map);
      }
      provider.insert(Events.DELETED_CONTENT_URI, map);
      return;
    }

    int entryState = entryToContentValues(event, syncLocalId, map, syncInfo);

    if (entryState == ENTRY_DELETED) {
      if (Config.LOGV) {
        Log.v(TAG, "Got deleted entry from server: " + map);
      }
      provider.insert(Events.DELETED_CONTENT_URI, map);
    } else if (entryState == ENTRY_OK) {
      if (Config.LOGV) {
        Log.v(TAG, "Got entry from server: " + map);
      }
      Uri result = provider.insert(Events.CONTENT_URI, map);
      long rowId = ContentUris.parseId(result);
      // handle the reminders for the event
      Integer hasAlarm = map.getAsInteger(Events.HAS_ALARM);
      if (hasAlarm != null && hasAlarm == 1) {
        // reminders should not be null
        Vector alarms = event.getReminders();
        if (alarms == null) {
          Log.e(TAG, "Have an alarm but do not have any reminders " + "-- should not happen.");
          throw new IllegalStateException("Have an alarm but do not have any reminders");
        }
        Enumeration reminders = alarms.elements();
        while (reminders.hasMoreElements()) {
          ContentValues reminderValues = new ContentValues();
          reminderValues.put(Calendar.Reminders.EVENT_ID, rowId);

          Reminder reminder = (Reminder) reminders.nextElement();
          byte method = reminder.getMethod();
          switch (method) {
            case Reminder.METHOD_DEFAULT:
              reminderValues.put(Calendar.Reminders.METHOD, Calendar.Reminders.METHOD_DEFAULT);
              break;
            case Reminder.METHOD_ALERT:
              reminderValues.put(Calendar.Reminders.METHOD, Calendar.Reminders.METHOD_ALERT);
              break;
            case Reminder.METHOD_EMAIL:
              reminderValues.put(Calendar.Reminders.METHOD, Calendar.Reminders.METHOD_EMAIL);
              break;
            case Reminder.METHOD_SMS:
              reminderValues.put(Calendar.Reminders.METHOD, Calendar.Reminders.METHOD_SMS);
              break;
            default:
              // should not happen.  return false?  we'd have to
              // roll back the event.
              Log.e(TAG, "Unknown reminder method: " + method + " should not happen!");
          }

          int minutes = reminder.getMinutes();
          reminderValues.put(
              Calendar.Reminders.MINUTES,
              minutes == Reminder.MINUTES_DEFAULT ? Calendar.Reminders.MINUTES_DEFAULT : minutes);

          if (provider.insert(Calendar.Reminders.CONTENT_URI, reminderValues) == null) {
            throw new ParseException("Unable to insert reminders.");
          }
        }
      }

      // handle attendees for the event
      Vector attendees = event.getAttendees();
      Enumeration attendeesEnum = attendees.elements();
      while (attendeesEnum.hasMoreElements()) {
        Who who = (Who) attendeesEnum.nextElement();
        ContentValues attendeesValues = new ContentValues();
        attendeesValues.put(Calendar.Attendees.EVENT_ID, rowId);
        attendeesValues.put(Calendar.Attendees.ATTENDEE_NAME, who.getValue());
        attendeesValues.put(Calendar.Attendees.ATTENDEE_EMAIL, who.getEmail());

        byte status;
        switch (who.getStatus()) {
          case Who.STATUS_NONE:
            status = Calendar.Attendees.ATTENDEE_STATUS_NONE;
            break;
          case Who.STATUS_INVITED:
            status = Calendar.Attendees.ATTENDEE_STATUS_INVITED;
            break;
          case Who.STATUS_ACCEPTED:
            status = Calendar.Attendees.ATTENDEE_STATUS_ACCEPTED;
            break;
          case Who.STATUS_TENTATIVE:
            status = Calendar.Attendees.ATTENDEE_STATUS_TENTATIVE;
            break;
          case Who.STATUS_DECLINED:
            status = Calendar.Attendees.ATTENDEE_STATUS_DECLINED;
            break;
          default:
            Log.w(TAG, "Unknown attendee status " + who.getStatus());
            status = Calendar.Attendees.ATTENDEE_STATUS_NONE;
        }
        attendeesValues.put(Calendar.Attendees.ATTENDEE_STATUS, status);
        byte rel;
        switch (who.getRelationship()) {
          case Who.RELATIONSHIP_NONE:
            rel = Calendar.Attendees.RELATIONSHIP_NONE;
            break;
          case Who.RELATIONSHIP_ORGANIZER:
            rel = Calendar.Attendees.RELATIONSHIP_ORGANIZER;
            break;
          case Who.RELATIONSHIP_ATTENDEE:
            rel = Calendar.Attendees.RELATIONSHIP_ATTENDEE;
            break;
          case Who.RELATIONSHIP_PERFORMER:
            rel = Calendar.Attendees.RELATIONSHIP_PERFORMER;
            break;
          case Who.RELATIONSHIP_SPEAKER:
            rel = Calendar.Attendees.RELATIONSHIP_SPEAKER;
            break;
          default:
            Log.w(TAG, "Unknown attendee relationship " + who.getRelationship());
            rel = Calendar.Attendees.RELATIONSHIP_NONE;
        }

        attendeesValues.put(Calendar.Attendees.ATTENDEE_RELATIONSHIP, rel);

        byte type;
        switch (who.getType()) {
          case Who.TYPE_NONE:
            type = Calendar.Attendees.TYPE_NONE;
            break;
          case Who.TYPE_REQUIRED:
            type = Calendar.Attendees.TYPE_REQUIRED;
            break;
          case Who.TYPE_OPTIONAL:
            type = Calendar.Attendees.TYPE_OPTIONAL;
            break;
          default:
            Log.w(TAG, "Unknown attendee type " + who.getType());
            type = Calendar.Attendees.TYPE_NONE;
        }
        attendeesValues.put(Calendar.Attendees.ATTENDEE_TYPE, type);
        if (provider.insert(Calendar.Attendees.CONTENT_URI, attendeesValues) == null) {
          throw new ParseException("Unable to insert attendees.");
        }
      }

      // handle the extended properties for the event
      Integer hasExtendedProperties = map.getAsInteger(Events.HAS_EXTENDED_PROPERTIES);
      if (hasExtendedProperties != null && hasExtendedProperties.intValue() != 0) {
        // extended properties should not be null
        // TODO: make the extended properties a bit more OO?
        Hashtable extendedProperties = event.getExtendedProperties();
        if (extendedProperties == null) {
          Log.e(
              TAG,
              "Have extendedProperties but do not have any properties" + "-- should not happen.");
          throw new IllegalStateException("Have extendedProperties but do not have any properties");
        }
        Enumeration propertyNames = extendedProperties.keys();
        while (propertyNames.hasMoreElements()) {
          String propertyName = (String) propertyNames.nextElement();
          String propertyValue = (String) extendedProperties.get(propertyName);
          ContentValues extendedPropertyValues = new ContentValues();
          extendedPropertyValues.put(Calendar.ExtendedProperties.EVENT_ID, rowId);
          extendedPropertyValues.put(Calendar.ExtendedProperties.NAME, propertyName);
          extendedPropertyValues.put(Calendar.ExtendedProperties.VALUE, propertyValue);
          if (provider.insert(Calendar.ExtendedProperties.CONTENT_URI, extendedPropertyValues)
              == null) {
            throw new ParseException("Unable to insert extended properties.");
          }
        }
      }
    } else {
      // If the DTSTART == -1, then the date was out of range.  We don't
      // need to throw a ParseException because the user can create
      // dates on the web that we can't handle on the phone.  For
      // example, events with dates before Dec 13, 1901 can be created
      // on the web but cannot be handled on the phone.
      Long dtstart = map.getAsLong(Events.DTSTART);
      if (dtstart != null && dtstart == -1) {
        return;
      }

      if (Config.LOGV) {
        Log.v(TAG, "Got invalid entry from server: " + map);
      }
      throw new ParseException("Got invalid entry from server: " + map);
    }
  }