Skip to content

Commit

Permalink
Merge pull request #1 from NesterovichAlexey/test3
Browse files Browse the repository at this point in the history
Test3
  • Loading branch information
NesterovichAlexey authored May 15, 2017
2 parents 8b48e40 + 0f7ea93 commit b265bfa
Show file tree
Hide file tree
Showing 16 changed files with 535 additions and 209 deletions.
Binary file modified MyLauncher-debug.apk
Binary file not shown.
23 changes: 23 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,26 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alexey.mylauncher">

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<permission
android:name="com.example.alexey.mylauncher.uri"
android:protectionLevel="dangerous"
android:label="Allows access to uri history"/>

<permission
android:name="com.example.alexey.mylauncher.uri.WRITE"
android:protectionLevel="dangerous"
android:label="Allows to add uri"/>

<permission
android:name="com.example.alexey.mylauncher.uri.READ_ALL"
android:protectionLevel="dangerous"
android:label="Allows access to all uri"/>

<application
android:name=".MyApplication"
android:allowBackup="true"
Expand Down Expand Up @@ -71,6 +87,13 @@
<data android:scheme="package" />
</intent-filter>
</receiver>

<provider
android:name=".MyContentProvider"
android:authorities="com.example.alexey.mylauncher"
android:exported="true"
android:permission="com.example.alexey.mylauncher.uri"
android:writePermission="com.example.alexey.mylauncher.uri.WRITE"/>
</application>

</manifest>
128 changes: 128 additions & 0 deletions app/src/main/java/com/example/alexey/mylauncher/MyContentProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.example.alexey.mylauncher;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Binder;
import android.os.Process;
import android.support.annotation.NonNull;
import android.support.v4.content.PermissionChecker;

import com.example.alexey.mylauncher.database.UriDatabaseHelper;

public class MyContentProvider extends ContentProvider {
private static final int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
private static final String AUTHORITY = "com.example.alexey.mylauncher";
private static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/uri");

private static final String ALL_URI_PATH = "uri";
private static final int ALL_URI_CODE = 1;
private static final String LAST_URI_PATH = "uri/last";
private static final int LAST_URI_CODE = 2;
private static final String TODAY_URI_PATH = "uri/today";
private static final int TODAY_URI_CODE = 3;
private static final String INSERT_URI_PATH = "uri/insert";
private static final int INSERT_URI_CODE = 4;
private static final String CLEAR_URI_PATH = "uri/clear";
private static final int CLEAR_URI_CODE = 5;

private static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, ALL_URI_PATH, ALL_URI_CODE);
uriMatcher.addURI(AUTHORITY, LAST_URI_PATH, LAST_URI_CODE);
uriMatcher.addURI(AUTHORITY, TODAY_URI_PATH, TODAY_URI_CODE);
uriMatcher.addURI(AUTHORITY, INSERT_URI_PATH, INSERT_URI_CODE);
uriMatcher.addURI(AUTHORITY, CLEAR_URI_PATH, CLEAR_URI_CODE);
}

private UriDatabaseHelper dbHelper;

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
if (uriMatcher.match(uri) != CLEAR_URI_CODE)
throw new IllegalArgumentException("Wrong URI: " + uri);
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete(UriDatabaseHelper.TABLE_NAME, null, null);
db.close();
return 0;
}

@Override
public String getType(Uri uri) {
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public Uri insert(Uri uri, ContentValues values) {
if (uriMatcher.match(uri) != INSERT_URI_CODE)
throw new IllegalArgumentException("Wrong URI: " + uri);
if (values.getAsString(UriDatabaseHelper.Columns.FIELD_URI_NAME) == null)
throw new IllegalArgumentException("Wrong values");
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete(UriDatabaseHelper.TABLE_NAME,
UriDatabaseHelper.Columns.FIELD_URI_NAME + " = ?",
new String[]{values.getAsString(UriDatabaseHelper.Columns.FIELD_URI_NAME)});
ContentValues cv = new ContentValues();
cv.put(UriDatabaseHelper.Columns.FIELD_URI_NAME, values.getAsString(UriDatabaseHelper.Columns.FIELD_URI_NAME));
cv.put(UriDatabaseHelper.Columns.FIELD_URI_TIME, System.currentTimeMillis());
long id = db.insert(UriDatabaseHelper.TABLE_NAME, null, cv);
db.close();
return ContentUris.withAppendedId(CONTENT_URI, id);
}

@Override
public boolean onCreate() {
dbHelper = new UriDatabaseHelper(getContext());
return true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
String tableName = UriDatabaseHelper.TABLE_NAME;
projection = new String[]{UriDatabaseHelper.Columns.FIELD_URI_NAME};
sortOrder = UriDatabaseHelper.Columns.FIELD_URI_TIME + " DESC";
String limits = null;
switch (uriMatcher.match(uri)) {
case ALL_URI_CODE:
if (getContext() == null || checkCallingPermission(getContext(), Manifest.permission.READ_ALL, null) != PermissionChecker.PERMISSION_GRANTED)
throw new SecurityException("Permission Denial");
selection = null;
selectionArgs = null;
break;
case LAST_URI_CODE:
selection = null;
selectionArgs = null;
limits = "1";
break;
case TODAY_URI_CODE:
selection = UriDatabaseHelper.Columns.FIELD_URI_TIME + " >= ?";
selectionArgs = new String[]{String.valueOf(System.currentTimeMillis() / MILLIS_IN_DAY * MILLIS_IN_DAY)};
break;
default:
throw new IllegalArgumentException("Wrong URI: " + uri);
}
return db.query(tableName, projection, selection, selectionArgs, null, null, sortOrder, limits);
}

private int checkCallingPermission(@NonNull Context context, @NonNull String permission, String packageName) {
if (Binder.getCallingPid() == Process.myPid()) {
return PackageManager.PERMISSION_GRANTED;
}
return PermissionChecker.checkCallingPermission(context, permission, packageName);
}

@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import android.provider.BaseColumns;
import android.util.Pair;

import com.example.alexey.mylauncher.recyclerview.AppInfo;
import com.example.alexey.mylauncher.recyclerview.ElementInfo;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -79,15 +79,15 @@ public HashMap<String, Pair<Integer, Boolean>> readRecords() {
return map;
}

public void writeRecords(ArrayList<AppInfo> appList) {
public void writeRecords(ArrayList<ElementInfo> appList) {
try {
SQLiteDatabase db = getWritableDatabase();
db.delete(TABLE_NAME, null, null);
for (AppInfo appInfo : appList) {
for (ElementInfo elementInfo : appList) {
ContentValues contentValues = new ContentValues();
contentValues.put(Columns.FIELD_PACKAGE_NAME, appInfo.packageName);
contentValues.put(Columns.FIELD_CLICK_COUNT, appInfo.clickCount);
contentValues.put(Columns.FIELD_IS_FAVORITES, appInfo.isFavorites ? 1 : 0);
contentValues.put(Columns.FIELD_PACKAGE_NAME, elementInfo.packageName);
contentValues.put(Columns.FIELD_CLICK_COUNT, elementInfo.clickCount);
contentValues.put(Columns.FIELD_IS_FAVORITES, elementInfo.isFavorites ? 1 : 0);
db.insert(TABLE_NAME, null, contentValues);
}
db.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.example.alexey.mylauncher.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

import com.example.alexey.mylauncher.recyclerview.ElementInfo;

import java.util.ArrayList;


public class ContactsDatabaseHelper extends SQLiteOpenHelper {
public static final int VERSION = 1;
public static final String DB_NAME = "contacts.dp";
public static final String TABLE_NAME = "contacts";

public static interface Columns extends BaseColumns {
String FIELD_NAME = "name";
String FIELD_PHONE_NUMBER = "phone_number";
String FIELD_CONTACT_ID = "contact_id";
}
public static final String CREATE_TABLE_SCRIPT =
"CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
"(" +
Columns.FIELD_NAME + " TEXT, " +
Columns.FIELD_PHONE_NUMBER + " TEXT, " +
Columns.FIELD_CONTACT_ID + " TEXT " +
")";
public static final String DROP_TABLE_SCRIPT =
"DROP TABLE IF EXISTS " + TABLE_NAME;

public ContactsDatabaseHelper(Context context) {
super(context, DB_NAME, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_SCRIPT);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE_SCRIPT);
onCreate(db);
}

@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}

public ArrayList<ElementInfo> readRecords() {
ArrayList<ElementInfo> map = new ArrayList<>();
try {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(
TABLE_NAME,
new String[]{Columns.FIELD_NAME, Columns.FIELD_PHONE_NUMBER, Columns.FIELD_CONTACT_ID},
null, null,
null, null, null, null
);
int idColumnName = cursor.getColumnIndex(Columns.FIELD_NAME);
int idColumnPhoneNumber = cursor.getColumnIndex(Columns.FIELD_PHONE_NUMBER);
int idColumnContactId = cursor.getColumnIndex(Columns.FIELD_CONTACT_ID);
while (cursor.moveToNext()) {
String name = cursor.getString(idColumnName);
String phoneNumber = cursor.getString(idColumnPhoneNumber);
ElementInfo elementInfo = new ElementInfo(name, phoneNumber, null, false);
elementInfo.contactId = cursor.getString(idColumnContactId);
map.add(elementInfo);
}
cursor.close();
} catch (SQLiteException ignored) {}
return map;
}

public void writeRecords(ArrayList<ElementInfo> contactsList) {
try {
SQLiteDatabase db = getWritableDatabase();
db.delete(TABLE_NAME, null, null);
for (ElementInfo elementInfo : contactsList) {
ContentValues contentValues = new ContentValues();
contentValues.put(Columns.FIELD_NAME, (String) elementInfo.appName);
contentValues.put(Columns.FIELD_PHONE_NUMBER, elementInfo.packageName);
contentValues.put(Columns.FIELD_CONTACT_ID, elementInfo.contactId);
db.insert(TABLE_NAME, null, contentValues);
}
db.close();
} catch (SQLiteException ignored) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public class UriDatabaseHelper extends SQLiteOpenHelper {

public static interface Columns extends BaseColumns {
String FIELD_URI_NAME = "uri_name";
String FIELD_URI_TIME = "uri_time";
}
public static final String CREATE_TABLE_SCRIPT =
"CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
"(" +
Columns.FIELD_URI_NAME + " TEXT" +
Columns.FIELD_URI_NAME + " TEXT, " +
Columns.FIELD_URI_TIME + " NUMBER " +
")";
public static final String DROP_TABLE_SCRIPT =
"DROP TABLE IF EXISTS " + TABLE_NAME;
Expand All @@ -46,36 +48,4 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}

public ArrayList<String> readRecords() {
ArrayList<String> list = new ArrayList<>();
try {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(
TABLE_NAME,
new String[]{Columns.FIELD_URI_NAME},
null, null,
null, null, null, null
);
int idColumnUri = cursor.getColumnIndex(Columns.FIELD_URI_NAME);
while (cursor.moveToNext()) {
list.add(cursor.getString(idColumnUri));
}
cursor.close();
} catch (SQLiteException ignored) {}
return list;
}

public void writeRecords(ArrayList<String> uriList) {
try {
SQLiteDatabase db = getWritableDatabase();
db.delete(TABLE_NAME, null, null);
for (String uri : uriList) {
ContentValues contentValues = new ContentValues();
contentValues.put(Columns.FIELD_URI_NAME, uri);
db.insert(TABLE_NAME, null, contentValues);
}
db.close();
} catch (SQLiteException ignored) {}
}
}
Loading

0 comments on commit b265bfa

Please sign in to comment.