tlmarker1968

My content provide attempt

Oct 26th, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.09 KB | None | 0 0
  1. My Content Provider:
  2.  
  3. package com.phehossof.launcher;
  4.  
  5. import android.content.ContentProvider;
  6. import android.content.ContentUris;
  7. import android.content.ContentValues;
  8. import android.content.Context;
  9. import android.content.UriMatcher;
  10. import android.database.Cursor;
  11. import android.database.sqlite.SQLiteDatabase;
  12. import android.database.sqlite.SQLiteException;
  13. import android.database.sqlite.SQLiteOpenHelper;
  14. import android.database.sqlite.SQLiteQueryBuilder;
  15. import android.net.Uri;
  16.  
  17. import androidx.annotation.NonNull;
  18. import androidx.annotation.Nullable;
  19.  
  20. import java.util.HashMap;
  21. import java.util.Objects;
  22.  
  23. public class ProviderContent extends ContentProvider {
  24.     static final String PROVIDER_NAME ="com.phehossof.launcher.ProviderCotent";
  25.     static final String URL = "content://" + PROVIDER_NAME +"/acl";
  26.     static final Uri CONTENT_URI = Uri.parse(URL);
  27.     static final String id = "id";
  28.     static final String name = "name";
  29.     static final String grade = "grade";
  30.     static final String department = "department";
  31.     static final int uriCode = 1;
  32.     static final UriMatcher uriMatcher;
  33.  
  34.     private static HashMap<String, String> values;
  35.     static {
  36.         uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  37.         uriMatcher.addURI(PROVIDER_NAME, "acl", uriCode);
  38.         uriMatcher.addURI(PROVIDER_NAME, "acl/*", uriCode);
  39.     }
  40.  
  41.     @Override
  42.     public boolean onCreate() {
  43.         Context context = getContext();
  44.         DatabaseHelper dbHelper = new DatabaseHelper(context);
  45.         db = dbHelper.getWritableDatabase();
  46.         return db != null;
  47.     }
  48.  
  49.     @Nullable
  50.     @Override
  51.     public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
  52.         SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
  53.         qb.setTables(TABLE_NAME);
  54.         if (uriMatcher.match(uri) == uriCode) {
  55.             qb.setProjectionMap(values);
  56.         } else {
  57.             throw new IllegalArgumentException("Unknown URI " + uri);
  58.         }
  59.         if (sortOrder == null || Objects.equals(sortOrder, "")) {
  60.             sortOrder = id;
  61.         }
  62.         Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
  63.         c.setNotificationUri(Objects.requireNonNull(getContext()).getContentResolver(), uri);
  64.         return c;
  65.     }
  66.  
  67.     @Nullable
  68.     @Override
  69.     public String getType(@NonNull Uri uri) {
  70.         if (uriMatcher.match(uri) == uriCode) {
  71.             return "vnd.android.cursor/acl";
  72.         }
  73.         throw new IllegalArgumentException("Unsupported URI:" + uri);
  74.     }
  75.  
  76.     @Nullable
  77.     @Override
  78.     public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
  79.         long rowID = db.insert(TABLE_NAME,"", values);
  80.         if (rowID > 0) {
  81.             Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
  82.             Objects.requireNonNull(getContext()).getContentResolver().notifyChange(_uri, null);
  83.             return _uri;
  84.         }
  85.         throw new SQLiteException("Failed to add record into " + uri);
  86.     }
  87.  
  88.     @Override
  89.     public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
  90.         int count;
  91.         if (uriMatcher.match(uri) == uriCode) {
  92.             count = db.delete(TABLE_NAME, selection, selectionArgs);
  93.         } else {
  94.             throw new IllegalArgumentException("Unknown URI " + uri);
  95.         }
  96.         Objects.requireNonNull(getContext()).getContentResolver().notifyChange(uri, null);
  97.         return count;
  98.     }
  99.  
  100.     @Override
  101.     public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
  102.         int count;
  103.         if (uriMatcher.match(uri) == uriCode) {
  104.             count = db.update(TABLE_NAME, values, selection, selectionArgs);
  105.         } else {
  106.             throw new IllegalArgumentException("Unknown URI " + uri);
  107.         }
  108.         Objects.requireNonNull(getContext()).getContentResolver().notifyChange(uri, null);
  109.         return count;
  110.     }
  111.  
  112.     private SQLiteDatabase db;
  113.     static final String DATABASE_NAME = "EmpDB";
  114.     static final String TABLE_NAME = "Employees";
  115.     static final int DATABASE_VERSION = 1;
  116.     static final String CREATE_DB_TABLE = " CREATE TABLE " + TABLE_NAME
  117.             + " (id INTEGER PRIMARY KEY AUTOINCREMENT, "
  118.             + " name TEXT NOT NULL);";
  119.  
  120.  
  121.     private static class DatabaseHelper extends SQLiteOpenHelper {
  122.         DatabaseHelper(Context context) {
  123.             super(context, DATABASE_NAME, null, DATABASE_VERSION);
  124.         }
  125.  
  126.         @Override
  127.         public void onCreate(SQLiteDatabase db) {
  128.             db.execSQL(CREATE_DB_TABLE);
  129.         }
  130.  
  131.         @Override
  132.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  133.             db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  134.             onCreate(db);
  135.         }
  136.     }
  137. }
  138.  
  139. My Manifest:
  140.  
  141. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  142.     xmlns:tools="http://schemas.android.com/tools"
  143.     package="com.phehossof.launcher">
  144.     <uses-permission android:name="android.permission.INTERNET" />
  145.     <application
  146.         android:networkSecurityConfig="@xml/network_security_config"
  147.         android:icon="@mipmap/launcher_icon"
  148.         android:label="@string/app_name"
  149.         android:roundIcon="@mipmap/launcher_icon_round"
  150.         android:supportsRtl="true"
  151.         android:theme="@style/AppTheme"
  152.         tools:ignore="AllowBackup,GoogleAppIndexingWarning">
  153.         <activity
  154.             android:name=".ActivityHome"
  155.             android:label="Phoenix Launcher"
  156.             android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen"
  157.             android:launchMode="singleTask"
  158.             android:stateNotNeeded="true" >
  159.             <intent-filter>
  160.                 <action android:name="android.intent.action.MAIN" />
  161.                 <category android:name="android.intent.category.HOME" />
  162.                 <category android:name="android.intent.category.DEFAULT" />
  163.                 <category android:name="android.intent.category.LAUNCHER" />
  164.             </intent-filter>
  165.         </activity>
  166.         <activity
  167.             android:name="com.phehossof.launcher.ActivityApp"
  168.             android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
  169.         </activity>
  170.         <provider
  171.             android:authorities="com.phehossof.launcher.contentprovider"
  172.             android:name=".ProviderContent"
  173.             tools:ignore="ExportedContentProvider">
  174.         </provider>
  175.     </application>
  176. </manifest>
  177.  
  178. How I am calling my content provider:
  179.  
  180.                         ContentValues values = new ContentValues();
  181.                         values.put(ProviderContent.name, objectUser.getUsername());
  182.                         values.put(ProviderContent.grade, String.valueOf(objectUser.getGrade()));
  183.                         values.put(ProviderContent.department, String.valueOf(objectUser.getDepartment()));
  184.                         getContentResolver().insert(ProviderContent.CONTENT_URI, values);
Advertisement
Add Comment
Please, Sign In to add comment