Commit 0ec8f765 authored by Mohit Mali's avatar Mohit Mali Committed by Romain Hunault
Browse files

Implement content provider code for microG installation status

parent 92f387ab
......@@ -18,6 +18,8 @@
<uses-permission android:name="foundation.e.pwaplayer.provider.READ_WRITE" />
<uses-permission android:name="foundation.e.apps.micro.status.READ_WRITE" />
<application
android:allowBackup="true"
android:alwaysRetainTaskState="true"
......@@ -73,13 +75,14 @@
android:launchMode="singleInstance"
android:theme="@style/AppTheme1"
android:windowSoftInputMode="adjustResize" />
<receiver android:name=".MicrogUninstallListener"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="foundation.e.apps" />
</intent-filter>
</receiver>
<provider
android:name=".MicroGProvider"
android:authorities="foundation.e.apps.micro.status"
android:exported="true"
/>
</application>
</manifest>
\ No newline at end of file
......@@ -24,10 +24,12 @@ import android.annotation.SuppressLint
import android.content.*
import android.content.pm.PackageManager
import android.content.res.ColorStateList
import android.database.Cursor
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.preference.PreferenceManager
import android.util.Log
import android.util.TypedValue
import android.view.MenuItem
import android.widget.Toast
......@@ -54,6 +56,7 @@ import foundation.e.apps.updates.UpdatesFragment
import foundation.e.apps.updates.UpdatesManager
import foundation.e.apps.utils.Constants
import foundation.e.apps.utils.Constants.CURRENTLY_SELECTED_FRAGMENT_KEY
import foundation.e.apps.utils.PreferenceStorage
import kotlinx.android.synthetic.main.activity_main.*
......@@ -88,7 +91,6 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
disableCategoryIfOpenSource()
bottom_navigation_view.setOnNavigationItemSelectedListener{
if (selectFragment(it.itemId,it)) {
disableCategoryIfOpenSource()
......@@ -121,6 +123,19 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
openSearchFragment()
}
override fun onResume() {
super.onResume()
if (retrieveStatus() != null) {
if (retrieveStatus().equals("true")) {
PreferenceStorage(this).save(getString(R.string.prefs_microg_vrsn_installed), true)
} else {
PreferenceStorage(this).save(getString(R.string.prefs_microg_vrsn_installed), false)
}
} else {
PreferenceStorage(this).save(getString(R.string.prefs_microg_vrsn_installed), false)
}
}
private fun openSearchFragment() {
if (intent.getBooleanExtra(Constants.OPEN_SEARCH,false)) {
currentFragmentId = R.id.menu_search
......@@ -150,6 +165,19 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
}
private fun retrieveStatus(): String? {
var status: String? = null
val c: Cursor? = contentResolver.query(MicroGProvider.CONTENT_URI, null, "id=?", arrayOf("1"), "installStatus")
if (c!!.moveToFirst()) {
do {
status = c.getString(c.getColumnIndex("installStatus"))
} while (c.moveToNext())
}
c.close()
return status
}
private fun initialiseUpdatesWorker() {
UpdatesManager(applicationContext).startWorker()
......
package foundation.e.apps;
import java.util.HashMap;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
public class MicroGProvider extends ContentProvider {
public static final String PROVIDER_NAME = "foundation.e.apps.micro.status";
public static final String URL = "content://" + PROVIDER_NAME + "/cte";
public static final Uri CONTENT_URI = Uri.parse(URL);
public static final String id = "id";
public static final String installStatus = "installStatus";
public static final int uriCode = 1;
public static final UriMatcher uriMatcher;
private static HashMap<String, String> values;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "cte", uriCode);
uriMatcher.addURI(PROVIDER_NAME, "cte/*", uriCode);
}
@Override
public boolean onCreate() {
Context context = getContext();
DatabaseHelper dbHelper = new DatabaseHelper(context);
db = dbHelper.getWritableDatabase();
if (db != null) {
return true;
}
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(TABLE_NAME);
switch (uriMatcher.match(uri)) {
case uriCode:
qb.setProjectionMap(values);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
if (sortOrder == null || sortOrder.equals("")) {
sortOrder = installStatus;
}
Cursor c = qb.query(db, projection, selection, selectionArgs, null,
null, sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case uriCode:
return "vnd.android.cursor.dir/cte";
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
long rowID = db.insert(TABLE_NAME, "", values);
if (rowID > 0) {
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(_uri, null);
return _uri;
}
throw new SQLException("Failed to add a record into " + uri);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)) {
case uriCode:
count = db.delete(TABLE_NAME, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)) {
case uriCode:
count = db.update(TABLE_NAME, values, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
private SQLiteDatabase db;
static final String DATABASE_NAME = "microGDB";
static final String TABLE_NAME = "microgtable";
static final int DATABASE_VERSION = 1;
static final String CREATE_DB_TABLE = " CREATE TABLE " + TABLE_NAME
+ " (id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ " installStatus TEXT NOT NULL);";
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DB_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
package foundation.e.apps
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import foundation.e.apps.utils.PreferenceStorage
class MicrogUninstallListener : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
PreferenceStorage(context).save(context.getString(R.string.prefs_microg_vrsn_installed), false)
}
}
\ No newline at end of file
......@@ -185,19 +185,12 @@ class ApplicationViewHolder(private val activity: Activity, private val view: Vi
installButton?.isEnabled = false
}
State.NOT_UPDATED -> {
if (application?.packageName == Constants.MICROG_PACKAGE) {
installButton.setBackgroundResource(R.drawable.app_install_border_simple)
installButton.text = "Install"
installButton.setTextColor(this.accentColorOS)
} else {
installButton.setTextColor(Color.parseColor("#FAFAFA"))
//installButton!!.setBackgroundResource(R.drawable.app_install_border)
if (0 != this.accentColorOS) {
installButton!!.setBackgroundColor(this.accentColorOS)
} else {
installButton!!.setBackgroundResource(R.drawable.app_install_border)
}
}
installButton?.isEnabled = true
}
......
......@@ -22,10 +22,12 @@ import android.app.PendingIntent
import android.content.*
import android.content.pm.PackageInstaller
import android.content.pm.PackageManager
import android.database.Cursor
import android.net.Uri
import android.util.Log
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
import foundation.e.apps.MicroGProvider
import foundation.e.apps.R
import foundation.e.apps.XAPK.FsUtils.deleteFileOrDir
import foundation.e.apps.utils.Constants
......@@ -149,8 +151,30 @@ class Installer(private val packageName: String,
if (packageName == Constants.MICROG_PACKAGE) {
PreferenceStorage(context).save(context.getString(R.string.prefs_microg_vrsn_installed), true)
if (count(MicroGProvider.CONTENT_URI, context)) {
val values = ContentValues()
values.put(MicroGProvider.installStatus, "true")
val state=context.contentResolver.update(MicroGProvider.CONTENT_URI, values,
MicroGProvider.id + "=?", arrayOf("1"))
} else {
val values = ContentValues()
values.put(MicroGProvider.installStatus, "true")
val state=context.contentResolver.insert(MicroGProvider.CONTENT_URI, values);
}
}
}
}
}
fun count(uri: Uri, context: Context): Boolean {
val cursor: Cursor? = context.contentResolver.query(uri, arrayOf("id"),
null, null, null)
Log.e("TAG", "count: " + cursor?.count)
val status = cursor?.count!! > 0
cursor.close()
return status
}
}
......@@ -40,8 +40,10 @@ class OutdatedApplicationsFileReader(private val packageManager: PackageManager,
override fun doInBackground(vararg context: Context): ArrayList<Application> {
val applications = ArrayList<Application>()
val application: Application? = loadMicroGVersion(context[0])[0]
println("versionname::-"+ application?.basicData!!.packageName)
if (application != null && application.state != State.INSTALLED) {
println("versionname::-"+ application?.basicData!!.lastVersionNumber)
if (PreferenceStorage(context[0])
.getBoolean(context[0].getString(R.string.prefs_microg_vrsn_installed), false)
&& application.state == State.NOT_UPDATED) {
applications.addAll(loadMicroGVersion(context[0]))
}
try {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment