Skip to content

Commit

Permalink
Merge pull request #216 from elimu-ai/#214-Download-Applications-from…
Browse files Browse the repository at this point in the history
…-REST-API

#214 download applications from rest api
  • Loading branch information
nya-elimu authored Jun 11, 2020
2 parents a12dd53 + f346412 commit b268d86
Show file tree
Hide file tree
Showing 25 changed files with 874 additions and 10 deletions.
27 changes: 25 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ android {
versionName "2.3.0-SNAPSHOT"

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

javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "${projectDir}/schemas".toString()]
}
}
}

buildTypes {
Expand All @@ -23,19 +29,36 @@ android {
minifyEnabled false
}
}

compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'ai.elimu:model:2.0.14'
// https://github.com/elimu-ai/model
implementation 'ai.elimu:model:2.0.15'

implementation 'com.jakewharton.timber:timber:4.5.1'
implementation 'com.google.android.material:material:1.1.0'

// AndroidX
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.7.2'
implementation 'com.squareup.retrofit2:converter-gson:2.7.2'

// Room components
implementation "androidx.room:room-runtime:2.2.5"
annotationProcessor "androidx.room:room-compiler:2.2.5"
androidTestImplementation "androidx.room:room-testing:2.2.5"

testImplementation 'junit:junit:4.12'

androidTestImplementation 'androidx.test.ext:junit:1.1.1'
Expand Down
52 changes: 52 additions & 0 deletions app/schemas/ai.elimu.appstore.room.RoomDb/1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "4f7ae834a52f85a4fcbcfaa3d7d0653f",
"entities": [
{
"tableName": "Application",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`packageName` TEXT NOT NULL, `infrastructural` INTEGER, `applicationStatus` TEXT NOT NULL, `id` INTEGER, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "packageName",
"columnName": "packageName",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "infrastructural",
"columnName": "infrastructural",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "applicationStatus",
"columnName": "applicationStatus",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": false
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '4f7ae834a52f85a4fcbcfaa3d7d0653f')"
]
}
}
9 changes: 8 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ai.elimu.appstore">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<permission android:name="${applicationId}.provider.READ" />
Expand All @@ -12,7 +13,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
android:networkSecurityConfig="@xml/network_security_config">

<activity android:name=".MainActivity">
<intent-filter>
Expand All @@ -23,6 +25,11 @@

<activity android:name=".ui.SelectLanguageActivity" />

<activity android:name=".ui.applications.InitialSyncActivity" />

<activity android:name=".ui.applications.ApplicationListActivity"
android:theme="@style/AppTheme.NoActionBar" />

<receiver android:name=".receiver.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/java/ai/elimu/appstore/BaseApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import android.app.Application;
import android.util.Log;

import ai.elimu.appstore.util.SharedPreferencesHelper;
import ai.elimu.appstore.util.VersionHelper;
import ai.elimu.model.enums.Language;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import timber.log.Timber;

public class BaseApplication extends Application {
Expand All @@ -19,4 +23,29 @@ public void onCreate() {

VersionHelper.updateAppVersion(getApplicationContext());
}

public Retrofit getRetrofit() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(getRestUrl() + "/")
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}

/**
* E.g. "http://hin.test.elimu.ai" or "http://hin.elimu.ai"
*/
public String getBaseUrl() {
Language language = SharedPreferencesHelper.getLanguage(getApplicationContext());
String url = "http://" + language.getIsoCode();
if (BuildConfig.DEBUG) {
url += ".test";
}
url += ".elimu.ai";
return url;
}

public String getRestUrl() {
return getBaseUrl() + "/rest/v2";
}
}
7 changes: 5 additions & 2 deletions app/src/main/java/ai/elimu/appstore/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import androidx.appcompat.app.AppCompatActivity;

import ai.elimu.appstore.ui.SelectLanguageActivity;
import ai.elimu.appstore.ui.applications.InitialSyncActivity;
import ai.elimu.appstore.util.SharedPreferencesHelper;
import ai.elimu.model.enums.Language;
import timber.log.Timber;
Expand Down Expand Up @@ -33,8 +34,10 @@ protected void onStart() {
startActivity(intent);
finish();
} else {
// Download list of Applications from REST API
// TODO
// Redirect to Activity for downloading list of Applications from REST API
Intent intent = new Intent(getApplicationContext(), InitialSyncActivity.class);
startActivity(intent);
finish();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Cursor query(Uri uri, String[] projection, String selection, String[] sel
BaseApplication baseApplication = (BaseApplication) context;
Cursor cursor = null;
// TODO
cursor.setNotificationUri(context.getContentResolver(), uri);
// cursor.setNotificationUri(context.getContentResolver(), uri);
return cursor;
} else {
throw new IllegalArgumentException("Unknown URI: " + uri);
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/ai/elimu/appstore/rest/ApplicationsService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ai.elimu.appstore.rest;

import java.util.List;

import ai.elimu.model.v2.gson.application.ApplicationGson;
import retrofit2.Call;
import retrofit2.http.GET;

public interface ApplicationsService {

@GET("applications")
Call<List<ApplicationGson>> listApplications();
}
31 changes: 31 additions & 0 deletions app/src/main/java/ai/elimu/appstore/room/EnumConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ai.elimu.appstore.room;

import android.text.TextUtils;

import androidx.room.TypeConverter;

import ai.elimu.model.enums.admin.ApplicationStatus;

/**
* See https://developer.android.com/training/data-storage/room/referencing-data
*/
public class EnumConverter {

@TypeConverter
public static ApplicationStatus fromApplicationStatus(String value) {
ApplicationStatus applicationStatus = null;
if (!TextUtils.isEmpty(value)) {
applicationStatus = ApplicationStatus.valueOf(value);
}
return applicationStatus;
}

@TypeConverter
public static String toApplicationStatus(ApplicationStatus applicationStatus) {
String value = null;
if (applicationStatus != null) {
value = applicationStatus.toString();
}
return value;
}
}
25 changes: 25 additions & 0 deletions app/src/main/java/ai/elimu/appstore/room/GsonToRoomConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ai.elimu.appstore.room;

import ai.elimu.appstore.room.entity.Application;
import ai.elimu.model.v2.gson.application.ApplicationGson;

public class GsonToRoomConverter {

public static Application getApplication(ApplicationGson applicationGson) {
if (applicationGson == null) {
return null;
} else {
Application application = new Application();

// BaseEntity
application.setId(applicationGson.getId());

// Application
application.setPackageName(applicationGson.getPackageName());
application.setInfrastructural(applicationGson.isInfrastructural());
application.setApplicationStatus(applicationGson.getApplicationStatus());

return application;
}
}
}
55 changes: 55 additions & 0 deletions app/src/main/java/ai/elimu/appstore/room/RoomDb.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ai.elimu.appstore.room;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.TypeConverters;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import ai.elimu.appstore.room.dao.ApplicationDao;
import ai.elimu.appstore.room.entity.Application;

@Database(version = 1, entities = {Application.class})
@TypeConverters({EnumConverter.class})
public abstract class RoomDb extends RoomDatabase {

public abstract ApplicationDao applicationDao();

private static volatile RoomDb INSTANCE;

public static final ExecutorService databaseWriteExecutor = Executors.newFixedThreadPool(4);

public static RoomDb getDatabase(final Context context) {
if (INSTANCE == null) {
synchronized (RoomDb.class) {
if (INSTANCE == null) {
INSTANCE = Room
.databaseBuilder(
context.getApplicationContext(),
RoomDb.class,
"appstore_db"
)
// .addMigrations(
// MIGRATION_1_2
// )
.build();
}
}
}
return INSTANCE;
}

// private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
// @Override
// public void migrate(SupportSQLiteDatabase database) {
// Log.i(getClass().getName(), "migrate (1 --> 2)");
// String sql = "...";
// Log.i(getClass().getName(), "sql: " + sql);
// database.execSQL(sql);
// }
// };
}
26 changes: 26 additions & 0 deletions app/src/main/java/ai/elimu/appstore/room/dao/ApplicationDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ai.elimu.appstore.room.dao;

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import java.util.List;

import ai.elimu.appstore.room.entity.Application;

@Dao
public interface ApplicationDao {

@Insert
void insert(Application application);

@Query("SELECT * FROM Application a WHERE a.id = :id")
Application load(Long id);

@Query("SELECT * FROM Application a")
List<Application> loadAll();

@Update
void update(Application application);
}
Loading

0 comments on commit b268d86

Please sign in to comment.