Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ArmanKhanTech committed Apr 12, 2024
1 parent a96e58e commit 2a9c2d4
Show file tree
Hide file tree
Showing 22 changed files with 209 additions and 358 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.example.focusonme;

import static org.junit.Assert.assertEquals;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
Expand All @@ -21,6 +21,6 @@ public class ExampleInstrumentedTest {
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.example.focusonme", appContext.getPackageName());
assertEquals("com.example.achievix", appContext.getPackageName());
}
}
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@
<activity
android:name=".Activity.UsageOverviewActivity"
android:exported="false"
android:noHistory="true"
android:theme="@style/Theme.Achievix" />
<activity
android:name=".Activity.MainActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import androidx.appcompat.app.AppCompatActivity;

import com.android.achievix.R;
import com.android.achievix.Utility.NetworkUtil;
import com.android.achievix.Utility.UsageUtil;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
Expand All @@ -39,7 +38,7 @@ public class AppStatsActivity extends AppCompatActivity {
private String appCategory;
private int appPosition;
private float dailyUsage, weeklyUsage, monthlyUsage, yearlyUsage, dataUsage;
private TextView dailyUsageTextView, weeklyUsageTextView, monthlyUsageTextView, yearlyUsageTextView, appPositionTextView, appCategoryTextView, dataUsageTextView;
private TextView dailyUsageTextView, weeklyUsageTextView, monthlyUsageTextView, yearlyUsageTextView, appPositionTextView, appCategoryTextView;
private BarChart usageBarChart;
private LinearLayout loadingLayout;

Expand All @@ -63,7 +62,6 @@ protected void onCreate(Bundle savedInstanceState) {
weeklyUsageTextView = findViewById(R.id.weekly_usage);
monthlyUsageTextView = findViewById(R.id.monthly_usage);
yearlyUsageTextView = findViewById(R.id.yearly_usage);
dataUsageTextView = findViewById(R.id.data_cons);
appPositionTextView = findViewById(R.id.app_rank);
appCategoryTextView = findViewById(R.id.app_cate);
loadingLayout = findViewById(R.id.ly1);
Expand All @@ -86,6 +84,7 @@ public void loadGraph() {
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

long startMillis = calendar.getTimeInMillis();
long endMillis = System.currentTimeMillis();
Expand Down Expand Up @@ -127,9 +126,10 @@ public void loadGraph() {
usageBarChart.getAxisLeft().setValueFormatter(new LargeValueFormatter());
usageBarChart.animateXY(2000, 2000);
usageBarChart.invalidate();
usageBarChart.getBarData().setValueTextSize(10f);
}

private void getStatsInfo(String pkgName) {
private void getStatsInfo() {
dailyUsage = usageUtil.getUsageByPackageName(this, packageName, "Daily");
weeklyUsage = usageUtil.getUsageByPackageName(this, packageName, "Weekly");
monthlyUsage = usageUtil.getUsageByPackageName(this, packageName, "Monthly");
Expand All @@ -139,14 +139,6 @@ private void getStatsInfo(String pkgName) {
weeklyUsage = weeklyUsage / (1000 * 60 * 60);
monthlyUsage = monthlyUsage / (1000 * 60 * 60);
yearlyUsage = yearlyUsage / (1000 * 60 * 60);

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

long startMillis = calendar.getTimeInMillis();
dataUsage = NetworkUtil.getUID(startMillis, System.currentTimeMillis(), pkgName, this);
}

private void getAppCategoryTitle() {
Expand All @@ -168,7 +160,7 @@ private static class TimeFormatter implements ValueFormatter {
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
int hours = (int) value;
int minutes = (int) ((value - hours) * 60);
return String.format("%02d:%02d", hours, minutes);
return String.format("%02d.%02d", hours, minutes);
}
}

Expand All @@ -182,7 +174,7 @@ protected void onPreExecute() {

@Override
protected Void doInBackground(Void... voids) {
getStatsInfo(packageName);
getStatsInfo();
getAppCategoryTitle();
return null;
}
Expand All @@ -193,15 +185,13 @@ protected void onPostExecute(Void results) {
super.onPostExecute(results);

loadingLayout.setVisibility(View.GONE);
dailyUsageTextView.setText(String.format("%02d:%02d", (int) dailyUsage, (int) ((dailyUsage - (int) dailyUsage) * 60)));
weeklyUsageTextView.setText(String.format("%02d:%02d", (int) weeklyUsage, (int) ((weeklyUsage - (int) weeklyUsage) * 60)));
monthlyUsageTextView.setText(String.format("%02d:%02d", (int) monthlyUsage, (int) ((monthlyUsage - (int) monthlyUsage) * 60)));
yearlyUsageTextView.setText(String.format("%02d:%02d", (int) yearlyUsage, (int) ((yearlyUsage - (int) yearlyUsage) * 60)));
dailyUsageTextView.setText(String.format("%02d.%02d", (int) dailyUsage, (int) ((dailyUsage - (int) dailyUsage) * 60)));
weeklyUsageTextView.setText(String.format("%02d.%02d", (int) weeklyUsage, (int) ((weeklyUsage - (int) weeklyUsage) * 60)));
monthlyUsageTextView.setText(String.format("%02d.%02d", (int) monthlyUsage, (int) ((monthlyUsage - (int) monthlyUsage) * 60)));
yearlyUsageTextView.setText(String.format("%02d.%02d", (int) yearlyUsage, (int) ((yearlyUsage - (int) yearlyUsage) * 60)));
appCategoryTextView.setText(appCategory != null ? appCategory : "Other");

appPositionTextView.setText(appPosition < 10 ? "0" + appPosition : appPosition > 99 ? "99+" : String.valueOf(appPosition));

dataUsageTextView.setText(String.valueOf(dataUsage));
loadGraph();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class BlockDataActivity : AppCompatActivity() {
"$data",
days.toString(),
null,
false,
true,
text
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,12 @@ class EditProfileActivity : AppCompatActivity() {
private lateinit var scheduleAdapter: ProfileScheduleAdapter
private lateinit var profileName: TextView
private lateinit var scheduleRecyclerView: RecyclerView
private lateinit var noSchedule: TextView
private lateinit var appRecyclerView: RecyclerView
private lateinit var addScheduleButton: ImageButton
private lateinit var addAppButton: ImageButton
private lateinit var noApp: TextView
private lateinit var webRecyclerView: RecyclerView
private lateinit var noWeb: TextView
private lateinit var addWebButton: ImageButton
private lateinit var keyRecyclerView: RecyclerView
private lateinit var noKey: TextView
private lateinit var addKeyButton: ImageButton
private lateinit var doneButton: Button
private var profileItemListApp = ArrayList<ProfileItemModel>()
Expand All @@ -59,23 +55,18 @@ class EditProfileActivity : AppCompatActivity() {
initWebRecyclerView()
initKeyRecyclerView()
attachListeners()
updateText()
}

private fun initializeView() {
profileName = findViewById(R.id.edit_profile_name)
profileName.text = intent.getStringExtra("profileName")
scheduleRecyclerView = findViewById(R.id.edit_profile_schedule_list)
noSchedule = findViewById(R.id.no_schedule)
appRecyclerView = findViewById(R.id.edit_profile_block_list_app)
addScheduleButton = findViewById(R.id.add_schedule)
addAppButton = findViewById(R.id.add_app)
noApp = findViewById(R.id.no_app)
webRecyclerView = findViewById(R.id.edit_profile_block_list_web)
noWeb = findViewById(R.id.no_web)
addWebButton = findViewById(R.id.add_web)
keyRecyclerView = findViewById(R.id.edit_profile_block_list_key)
noKey = findViewById(R.id.no_key)
addKeyButton = findViewById(R.id.add_key)
doneButton = findViewById(R.id.save_edit_profile_button)
}
Expand Down Expand Up @@ -245,7 +236,6 @@ class EditProfileActivity : AppCompatActivity() {
}

initWebRecyclerView()
updateText()
dialog.dismiss()
}
} else {
Expand Down Expand Up @@ -312,7 +302,6 @@ class EditProfileActivity : AppCompatActivity() {
}

initKeyRecyclerView()
updateText()
dialog.dismiss()
}
} else {
Expand All @@ -336,33 +325,6 @@ class EditProfileActivity : AppCompatActivity() {
}
}

@SuppressLint("SetTextI18n")
fun updateText() {
if (scheduleRecyclerView.adapter!!.itemCount == 0) {
noSchedule.text = "No Schedule"
} else {
noSchedule.text = ""
}

if (appRecyclerView.adapter!!.itemCount == 0) {
noApp.text = "No App"
} else {
noApp.text = ""
}

if (webRecyclerView.adapter!!.itemCount == 0) {
noWeb.text = "No Website"
} else {
noWeb.text = ""
}

if (keyRecyclerView.adapter!!.itemCount == 0) {
noKey.text = "No Keyword"
} else {
noKey.text = ""
}
}

@Deprecated("Deprecated in Java")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Expand All @@ -377,6 +339,7 @@ class EditProfileActivity : AppCompatActivity() {

for (j in scheduleModelList) {
if (app == "com.android.achievix") {
Toast.makeText(this, "Cannot block Achievix", Toast.LENGTH_SHORT).show()
continue
} else {
blockDatabase.addRecord(
Expand All @@ -397,7 +360,6 @@ class EditProfileActivity : AppCompatActivity() {
}

initAppRecyclerView()
updateText()
} else {
Toast.makeText(this, "Add a schedule first", Toast.LENGTH_SHORT).show()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;

public class KeywordBlockActivity extends AppCompatActivity {
Expand Down Expand Up @@ -130,7 +132,15 @@ private void getBlockedWebsite() {
if (webKeyModelList.isEmpty()) {
noKeyBlock.setVisibility(TextView.VISIBLE);
} else {
webKeyBlockAdapter = new WebKeyBlockAdapter(webKeyModelList, false);
Set<String> keys = new HashSet<>();
List<WebKeyModel> filteredList = new ArrayList<>();

for (WebKeyModel model : webKeyModelList) {
if (keys.add(model.getName())) {
filteredList.add(model);
}
}
webKeyBlockAdapter = new WebKeyBlockAdapter(filteredList, false);
recyclerView.setAdapter(webKeyBlockAdapter);

webKeyBlockAdapter.setOnItemClickListener(view -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SwitchCompat
import com.android.achievix.Database.BlockDatabase
import com.android.achievix.R
import java.util.Calendar

class QuickBlockActivity : AppCompatActivity() {
private lateinit var launchSwitch: SwitchCompat
Expand Down Expand Up @@ -168,15 +169,15 @@ class QuickBlockActivity : AppCompatActivity() {
}

private fun getCurrentDay(): List<String> {
return when (System.currentTimeMillis() % 7) {
0L -> mutableListOf("Sunday")
1L -> mutableListOf("Monday")
2L -> mutableListOf("Tuesday")
3L -> mutableListOf("Wednesday")
4L -> mutableListOf("Thursday")
5L -> mutableListOf("Friday")
6L -> mutableListOf("Saturday")
else -> mutableListOf("Sunday")
return when (Calendar.getInstance().get(Calendar.DAY_OF_WEEK)) {
Calendar.SUNDAY -> mutableListOf("Sunday")
Calendar.MONDAY -> mutableListOf("Monday")
Calendar.TUESDAY -> mutableListOf("Tuesday")
Calendar.WEDNESDAY -> mutableListOf("Wednesday")
Calendar.THURSDAY -> mutableListOf("Thursday")
Calendar.FRIDAY -> mutableListOf("Friday")
Calendar.SATURDAY -> mutableListOf("Saturday")
else -> mutableListOf("")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void repair(View v) {
Intent serviceIntent = new Intent(this, ForegroundService.class);
serviceIntent.putExtra("inputExtra", "Foreground Service is Running");
ContextCompat.startForegroundService(this, serviceIntent);
Toast.makeText(this, "App Repaired", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "App repaired", Toast.LENGTH_SHORT).show();
}

public void repairLog(View v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;

public class WebBlockActivity extends AppCompatActivity {
Expand Down Expand Up @@ -131,7 +133,15 @@ private void getBlockedWebsite() {
if (webKeyModelList.isEmpty()) {
noWebBlock.setVisibility(TextView.VISIBLE);
} else {
webKeyBlockAdapter = new WebKeyBlockAdapter(webKeyModelList, true);
Set<String> urls = new HashSet<>();
List<WebKeyModel> filteredList = new ArrayList<>();

for (WebKeyModel model : webKeyModelList) {
if (urls.add(model.getName())) {
filteredList.add(model);
}
}
webKeyBlockAdapter = new WebKeyBlockAdapter(filteredList, true);
recyclerView.setAdapter(webKeyBlockAdapter);

webKeyBlockAdapter.setOnItemClickListener(view -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ class ProfileAdapter(private var profileList: List<ProfileModel>) :
holder.profileName.text = profileInfo.profileName
var status = profileInfo.status == "1"
holder.profileAction.setImageResource(if (status) R.drawable.pause_icon_pink else R.drawable.play_icon_pink)
if (status) {
holder.profileName.setPadding(0, 0, 0, 0)
}
holder.profileAction.setOnClickListener {
BlockDatabase(holder.itemView.context).toggleProfile(profileInfo.profileName, status)
status = !status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class ProfileItemAdapter(
blockDatabase.deleteProfileItem(itemInfo.profileName, itemInfo.name)
itemList = itemList.filter { it.id != itemInfo.id }
notifyItemRemoved(position)
activity.updateText()
}
}

Expand Down
Loading

0 comments on commit 2a9c2d4

Please sign in to comment.