Skip to content

Commit

Permalink
Adds structures to display descriptor reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
aanorbel committed Jan 18, 2024
1 parent 9907849 commit e543532
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 16 deletions.
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
android:exported="false"
android:parentActivityName=".activity.MainActivity"
android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar.App.NoActionBar" />
<activity
android:name=".activity.reviewdescriptorupdates.ReviewDescriptorUpdatesActivity"
android:exported="false"
android:parentActivityName=".activity.MainActivity"
android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar.App.NoActionBar" />
<activity
android:name=".activity.OnboardingActivity"
android:screenOrientation="userPortrait"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.openobservatory.engine.OONIRunDescriptor;
import org.openobservatory.ooniprobe.R;
import org.openobservatory.ooniprobe.activity.adddescriptor.AddDescriptorActivity;
import org.openobservatory.ooniprobe.activity.reviewdescriptorupdates.ReviewDescriptorUpdatesActivity;
import org.openobservatory.ooniprobe.common.*;
import org.openobservatory.ooniprobe.common.service.ServiceUtil;
import org.openobservatory.ooniprobe.common.worker.AutoUpdateDescriptorsWorker;
Expand Down Expand Up @@ -168,15 +169,15 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
private void scheduleWorkers() {
WorkManager.getInstance(this)
.enqueueUniquePeriodicWork(
AutoUpdateDescriptorsWorker.UPDATED_DESCRIPTORS_WORK_NAME,
ExistingPeriodicWorkPolicy.KEEP,
new PeriodicWorkRequest.Builder(AutoUpdateDescriptorsWorker.class, 24, TimeUnit.HOURS)
.setConstraints(
new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
).build()
);
AutoUpdateDescriptorsWorker.UPDATED_DESCRIPTORS_WORK_NAME,
ExistingPeriodicWorkPolicy.KEEP,
new PeriodicWorkRequest.Builder(AutoUpdateDescriptorsWorker.class, 24, TimeUnit.HOURS)
.setConstraints(
new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
).build()
);

OneTimeWorkRequest manualWorkRequest = new OneTimeWorkRequest.Builder(ManualUpdateDescriptorsWorker.class)
.setConstraints(
Expand All @@ -198,6 +199,12 @@ private void scheduleWorkers() {
if (workInfo.getState().isFinished()) {
if (workInfo.getState() == WorkInfo.State.SUCCEEDED) {
System.out.println(workInfo.getOutputData());
startActivity(
ReviewDescriptorUpdatesActivity.newIntent(
this,
workInfo.getOutputData().getString(ManualUpdateDescriptorsWorker.KEY_UPDATED_DESCRIPTORS)
)
);
} else {
System.out.println("Failed");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package org.openobservatory.ooniprobe.activity.reviewdescriptorupdates

import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.google.gson.Gson
import org.openobservatory.ooniprobe.R
import org.openobservatory.ooniprobe.activity.AbstractActivity
import org.openobservatory.ooniprobe.common.PreferenceManager
import org.openobservatory.ooniprobe.common.TestDescriptorManager
import org.openobservatory.ooniprobe.databinding.ActivityReviewDescriptorUpdatesBinding
import org.openobservatory.ooniprobe.databinding.FragmentDescriptorUpdateBinding
import org.openobservatory.ooniprobe.model.database.InstalledDescriptor
import org.openobservatory.ooniprobe.model.database.TestDescriptor
import javax.inject.Inject

class ReviewDescriptorUpdatesActivity : AbstractActivity() {

companion object {
private const val DESCRIPTORS = "descriptors"

/**
* This method is used to create an intent to start this activity.
* @param context is the context of the activity that calls this method
* @param descriptors is the descriptors to review
* @return an intent to start this activity
*/
@JvmStatic
fun newIntent(context: Context, descriptors: String?): Intent {
return Intent(context, ReviewDescriptorUpdatesActivity::class.java).putExtra(
DESCRIPTORS,
descriptors
)
}
}

@Inject
lateinit var preferenceManager: PreferenceManager

@Inject
lateinit var descriptorManager: TestDescriptorManager

@Inject
lateinit var gson: Gson

private lateinit var reviewUpdatesPagingAdapter: ReviewUpdatesPagingAdapter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityComponent.inject(this)
val binding = ActivityReviewDescriptorUpdatesBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(false)
supportActionBar?.setDisplayShowHomeEnabled(false)
supportActionBar?.title = "Link Update"
val descriptorJson = intent.getStringExtra(DESCRIPTORS)
try {
val descriptors: Array<TestDescriptor> =
gson.fromJson(descriptorJson, Array<TestDescriptor>::class.java)

binding.viewpager.isUserInputEnabled = false
reviewUpdatesPagingAdapter = ReviewUpdatesPagingAdapter(this, descriptors.toList())
binding.viewpager.adapter = reviewUpdatesPagingAdapter


val bottomBarOnMenuItemClickListener: Toolbar.OnMenuItemClickListener =
Toolbar.OnMenuItemClickListener { item ->
when (item.itemId) {
R.id.update_descriptor -> {
val currPos: Int = binding.viewpager.currentItem
if ((currPos + 1) != binding.viewpager.adapter?.itemCount) {
binding.viewpager.currentItem = currPos + 1
} else {
/*preferenceManager.setLastUpdateDescriptorReview(
descriptorManager.getDescriptorVersion()
)*/
finish()
}
true
}

else -> false
}
}
binding.bottomBar.setOnMenuItemClickListener(bottomBarOnMenuItemClickListener)

binding.viewpager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
binding.bottomBar.menu.findItem(R.id.update_descriptor)
?.setTitle("UPDATE (${position + 1} of ${binding.viewpager.adapter?.itemCount})")

}
})

} catch (e: Exception) {
finish()
}
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater: MenuInflater = menuInflater
inflater.inflate(R.menu.close, menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.close_button -> {
finish()
true
}

else -> super.onOptionsItemSelected(item)
}
}
}

class ReviewUpdatesPagingAdapter(
fragmentActivity: FragmentActivity,
private val descriptors: List<TestDescriptor>
) : FragmentStateAdapter(fragmentActivity) {
override fun getItemCount(): Int = descriptors.size

override fun createFragment(position: Int): Fragment {
val fragment = DescriptorUpdateFragment()
fragment.arguments = Bundle().apply {
putSerializable(DESCRIPTOR, descriptors[position])
}
return fragment
}
}

private const val DESCRIPTOR = "descriptor"

class DescriptorUpdateFragment : Fragment() {

private lateinit var binding: FragmentDescriptorUpdateBinding
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentDescriptorUpdateBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
arguments?.takeIf { it.containsKey(DESCRIPTOR) }?.apply {
val descriptor: TestDescriptor =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getSerializable(DESCRIPTOR, TestDescriptor::class.java)!!
} else {
getSerializable(DESCRIPTOR) as TestDescriptor
}
val absDescriptor = InstalledDescriptor(descriptor)
binding.apply {
title.text = absDescriptor.title
description.text = absDescriptor.description // Use markdown
icon.setImageResource(absDescriptor.getDisplayIcon(requireContext()))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ class ManualUpdateDescriptorsWorker(

private val UPDATE_DESCRIPTOR_CHANNEL: String =
AutoUpdateDescriptorsWorker::class.java.simpleName

private val KEY_UPDATED_DESCRIPTORS =
@JvmField
var KEY_UPDATED_DESCRIPTORS =
"${AutoUpdateDescriptorsWorker::class.java.name}.KEY_UPDATED_DESCRIPTORS"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.openobservatory.ooniprobe.activity.OverviewActivity;
import org.openobservatory.ooniprobe.activity.ProxyActivity;
import org.openobservatory.ooniprobe.activity.ResultDetailActivity;
import org.openobservatory.ooniprobe.activity.reviewdescriptorupdates.ReviewDescriptorUpdatesActivity;
import org.openobservatory.ooniprobe.activity.runtests.RunTestsActivity;
import org.openobservatory.ooniprobe.activity.RunningActivity;
import org.openobservatory.ooniprobe.activity.TextActivity;
Expand All @@ -20,6 +21,7 @@
@PerActivity
@Subcomponent()
public interface ActivityComponent {
void inject(ReviewDescriptorUpdatesActivity activity);
void inject(AddDescriptorActivity activity);
void inject(CustomWebsiteActivity activity);
void inject(MainActivity activity);
Expand Down
45 changes: 45 additions & 0 deletions app/src/main/res/layout/activity_review_descriptor_updates.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.reviewdescriptorupdates.ReviewDescriptorUpdatesActivity">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/color_gray0"
app:menu="@menu/close" />
</com.google.android.material.appbar.AppBarLayout>

<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
android:layout_marginBottom="?android:attr/actionBarSize"/>


<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar.App.NoActionBar">

<androidx.appcompat.widget.Toolbar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_gray0"
app:menu="@menu/update_descriptor"
app:titleTextAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title.App" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
82 changes: 82 additions & 0 deletions app/src/main/res/layout/fragment_descriptor_update.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="16dp"
android:paddingEnd="16dp"
tools:context=".activity.reviewdescriptorupdates.DescriptorUpdateFragment">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/settings_icon" />

<TextView
android:id="@+id/title"
style="?attr/textAppearanceHeadline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:ellipsize="end"
android:singleLine="true"
android:textFontWeight="600"
app:layout_constraintStart_toEndOf="@id/icon"
app:layout_constraintTop_toTopOf="parent"
tools:text="@string/Test_Websites_Fullname" />

<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/icon" />

<TextView
android:id="@+id/author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/divider"
tools:text="Autho" />

<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="8dp"
android:textColor="@color/color_gray6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/author"
tools:text="lore ipsum" />

<TextView
android:id="@+id/tests_llabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tests"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/description" />

<org.openobservatory.ooniprobe.common.views.CustomExpandableListView
android:id="@+id/expandable_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:groupIndicator="@null"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tests_llabel"
tools:listitem="@layout/nettest_group_list_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
Loading

0 comments on commit e543532

Please sign in to comment.