Skip to content

Commit

Permalink
Move TaskContract to separte module. #354 (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabor Keszthelyi authored and dmfs committed Jul 26, 2017
1 parent 583c099 commit 71630d4
Show file tree
Hide file tree
Showing 70 changed files with 327 additions and 247 deletions.
1 change: 1 addition & 0 deletions opentasks-contract/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
23 changes: 23 additions & 0 deletions opentasks-contract/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion COMPILE_SDK_VERSION.toInteger()
buildToolsVersion BUILD_TOOLS_VERSION

defaultConfig {
minSdkVersion MIN_SDK_VERSION.toInteger()
targetSdkVersion TARGET_SDK_VERSION.toInteger()

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
}
5 changes: 5 additions & 0 deletions opentasks-contract/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<manifest package="org.dmfs.tasks.contract">

<application/>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,15 @@
* limitations under the License.
*/

package org.dmfs.provider.tasks;
package org.dmfs.tasks.contract;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ProviderInfo;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.SyncStateContract;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -53,10 +47,6 @@
*/
public final class TaskContract
{
/**
* The task authority cache.
*/
private static Map<String, String> sAuthorities = Collections.synchronizedMap(new HashMap<String, String>(4));

private static Map<String, UriFactory> sUriFactories = new HashMap<String, UriFactory>(4);

Expand Down Expand Up @@ -169,7 +159,7 @@ private TaskContract()
*/
public static class SyncState implements SyncStateContract.Columns, BaseColumns
{
final static String CONTENT_URI_PATH = "syncstate";
public final static String CONTENT_URI_PATH = "syncstate";


/**
Expand All @@ -180,7 +170,7 @@ public static class SyncState implements SyncStateContract.Columns, BaseColumns
*
* @return A {@link Uri}.
*/
public final static Uri getContentUri(String authority)
public static Uri getContentUri(String authority)
{
return getUriFactory(authority).getUri(CONTENT_URI_PATH);
}
Expand All @@ -196,7 +186,7 @@ public final static Uri getContentUri(String authority)
*
* @return A {@link Uri}.
*/
public final static Uri getContentUri(String authority)
public static Uri getContentUri(String authority)
{
return getUriFactory(authority).getUri();
}
Expand Down Expand Up @@ -434,7 +424,7 @@ public interface TaskListColumns
*/
public static final class TaskLists implements TaskListColumns, TaskListSyncColumns, CommonSyncColumns
{
static final String CONTENT_URI_PATH = "tasklists";
public static final String CONTENT_URI_PATH = "tasklists";

/**
* The default sort order.
Expand All @@ -457,7 +447,7 @@ public static final class TaskLists implements TaskListColumns, TaskListSyncColu
*
* @return A {@link Uri}.
*/
public final static Uri getContentUri(String authority)
public static Uri getContentUri(String authority)
{
return getUriFactory(authority).getUri(CONTENT_URI_PATH);
}
Expand Down Expand Up @@ -944,11 +934,11 @@ public static final class Tasks implements TaskColumns, CommonSyncColumns, TaskS
*/
public static final String VISIBLE = "visible";

static final String CONTENT_URI_PATH = "tasks";
public static final String CONTENT_URI_PATH = "tasks";

static final String SEARCH_URI_PATH = "tasks_search";
public static final String SEARCH_URI_PATH = "tasks_search";

static final String SEARCH_QUERY_PARAMETER = "q";
public static final String SEARCH_QUERY_PARAMETER = "q";

public static final String DEFAULT_SORT_ORDER = DUE;

Expand All @@ -965,13 +955,13 @@ public static final class Tasks implements TaskColumns, CommonSyncColumns, TaskS
*
* @return A {@link Uri}.
*/
public final static Uri getContentUri(String authority)
public static Uri getContentUri(String authority)
{
return getUriFactory(authority).getUri(CONTENT_URI_PATH);
}


public final static Uri getSearchUri(String authority, String query)
public static Uri getSearchUri(String authority, String query)
{
Uri.Builder builder = getUriFactory(authority).getUri(SEARCH_URI_PATH).buildUpon();
builder.appendQueryParameter(SEARCH_QUERY_PARAMETER, Uri.encode(query));
Expand Down Expand Up @@ -1141,7 +1131,7 @@ public static final class Instances implements TaskColumns, InstanceColumns
*/
public static final String VISIBLE = "visible";

static final String CONTENT_URI_PATH = "instances";
public static final String CONTENT_URI_PATH = "instances";

public static final String DEFAULT_SORT_ORDER = INSTANCE_DUE_SORTING;

Expand All @@ -1154,7 +1144,7 @@ public static final class Instances implements TaskColumns, InstanceColumns
*
* @return A {@link Uri}.
*/
public final static Uri getContentUri(String authority)
public static Uri getContentUri(String authority)
{
return getUriFactory(authority).getUri(CONTENT_URI_PATH);
}
Expand Down Expand Up @@ -1187,7 +1177,7 @@ public interface CategoriesColumns
public static final class Categories implements CategoriesColumns
{

static final String CONTENT_URI_PATH = "categories";
public static final String CONTENT_URI_PATH = "categories";

public static final String DEFAULT_SORT_ORDER = NAME;

Expand All @@ -1200,7 +1190,7 @@ public static final class Categories implements CategoriesColumns
*
* @return A {@link Uri}.
*/
public final static Uri getContentUri(String authority)
public static Uri getContentUri(String authority)
{
return getUriFactory(authority).getUri(CONTENT_URI_PATH);
}
Expand All @@ -1221,7 +1211,7 @@ public interface AlarmsColumns
public static final class Alarms implements AlarmsColumns
{

static final String CONTENT_URI_PATH = "alarms";
public static final String CONTENT_URI_PATH = "alarms";


/**
Expand All @@ -1232,7 +1222,7 @@ public static final class Alarms implements AlarmsColumns
*
* @return A {@link Uri}.
*/
public final static Uri getContentUri(String authority)
public static Uri getContentUri(String authority)
{
return getUriFactory(authority).getUri(CONTENT_URI_PATH);
}
Expand Down Expand Up @@ -1308,7 +1298,7 @@ public interface PropertyColumns
public static final class Properties implements PropertySyncColumns, PropertyColumns
{

static final String CONTENT_URI_PATH = "properties";
public static final String CONTENT_URI_PATH = "properties";

public static final String DEFAULT_SORT_ORDER = DATA0;

Expand All @@ -1321,7 +1311,7 @@ public static final class Properties implements PropertySyncColumns, PropertyCol
*
* @return A {@link Uri}.
*/
public final static Uri getContentUri(String authority)
public static Uri getContentUri(String authority)
{
return getUriFactory(authority).getUri(CONTENT_URI_PATH);
}
Expand Down Expand Up @@ -1634,7 +1624,7 @@ public static interface Alarm extends PropertyColumns
public final static String MINUTES_BEFORE = DATA0;

/**
* The reference date for the alarm. Either {@link ALARM_REFERENCE_DUE_DATE} or {@link ALARM_REFERENCE_START_DATE}.
* The reference date for the alarm. Either {@link #ALARM_REFERENCE_DUE_DATE} or {@link #ALARM_REFERENCE_START_DATE}.
* <p>
* Value: Integer
* </p>
Expand All @@ -1650,8 +1640,8 @@ public static interface Alarm extends PropertyColumns
public final static String MESSAGE = DATA2;

/**
* The type of the alarm. Use the provided alarm types {@link ALARM_TYPE_MESSAGE}, {@link ALARM_TYPE_SOUND}, {@link ALARM_TYPE_NOTHING},
* {@link ALARM_TYPE_EMAIL} and {@link ALARM_TYPE_SMS}.
* The type of the alarm. Use the provided alarm types {@link #ALARM_TYPE_MESSAGE}, {@link #ALARM_TYPE_SOUND}, {@link #ALARM_TYPE_NOTHING},
* {@link #ALARM_TYPE_EMAIL} and {@link #ALARM_TYPE_SMS}.
* <p>
* Value: Integer
* </p>
Expand Down Expand Up @@ -1682,64 +1672,4 @@ private static synchronized UriFactory getUriFactory(String authority)
return uriFactory;
}


/**
* Returns the authority of the {@link TaskProvider} in the given {@link Context}.
* <p/>
* TODO: create an Authority class instead that handles everything about authorities. It could replace {@link UriFactory} as well. The Authority class could
* have a generic parameter that identifies the authority provider or contract class.
*
* @param context
* A {@link Context} of an app that contains a {@link TaskProvider}.
*
* @return The authority.
*
* @throws RuntimeException
* if there is no {@link TaskProvider} in that {@link Context}.
*/
public static synchronized String taskAuthority(Context context)
{
String packageName = context.getPackageName();
if (sAuthorities.containsKey(packageName))
{
return sAuthorities.get(packageName);
}

PackageManager packageManager = context.getPackageManager();

// first get the PackageInfo of this app.
PackageInfo packageInfo;
try
{
packageInfo = packageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_PROVIDERS);
}
catch (NameNotFoundException e)
{
throw new RuntimeException("Could not find TaskProvider!", e);
}

// next scan all providers for TaskProvider
for (ProviderInfo provider : packageInfo.providers)
{
Class<?> providerClass;
try
{
providerClass = Class.forName(provider.name);
}
catch (ClassNotFoundException e)
{
continue;
}

if (!TaskProvider.class.isAssignableFrom(providerClass))
{
continue;
}

sAuthorities.put(packageName, provider.authority);
return provider.authority;
}
throw new RuntimeException("Could not find TaskProvider! Make sure you added it to your AndroidManifest.xml.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,43 @@
* limitations under the License.
*/

package org.dmfs.provider.tasks;
package org.dmfs.tasks.contract;

import android.net.Uri;

import java.util.HashMap;
import java.util.Map;


public class UriFactory
/**
* TODO
*/
public final class UriFactory
{
public final String authority;

private final String mAuthority;
private final Map<String, Uri> mUriMap = new HashMap<String, Uri>(16);


UriFactory(String authority)
{
this.authority = authority;
mAuthority = authority;
mUriMap.put((String) null, Uri.parse("content://" + authority));
}


void addUri(String path)
{
mUriMap.put(path, Uri.parse("content://" + authority + "/" + path));
mUriMap.put(path, Uri.parse("content://" + mAuthority + "/" + path));
}


public Uri getUri()
Uri getUri()
{
return mUriMap.get(null);
}


public Uri getUri(String path)
Uri getUri(String path)
{
return mUriMap.get(path);
}
Expand Down
1 change: 1 addition & 0 deletions opentasks-provider/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ android {
dependencies {
compile('org.dmfs:rfc5545-datetime:0.2.4')
compile('org.dmfs:lib-recur:0.9.6')
compile project(':opentasks-contract')
testCompile 'junit:junit:4.12'
}
Loading

0 comments on commit 71630d4

Please sign in to comment.