Skip to content

Commit

Permalink
Some BugFixes Option to Aloow autostart on xiaomi and similar
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaiserdragon2 committed Dec 7, 2024
1 parent e246d96 commit 21f69b1
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 69 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "de.kaiserdragon.callforwardingstatus"
minSdk 28
targetSdk 35
versionCode 7
versionName "2.0dev"
versionCode 8
versionName "2.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import de.kaiserdragon.callforwardingstatus.helper.DatabaseHelper;


public class CallForwardingReceiver extends BroadcastReceiver {
final String TAG = "Receiver";

Expand All @@ -32,12 +31,15 @@ public void onReceive(Context context, Intent intent) {
DatabaseHelper databaseHelper = new DatabaseHelper(context);
String[] array = databaseHelper.getSelected();
databaseHelper.close();
//if (DEBUG) Log.v(TAG, "Number = " + array[1]);
if (!array[1].isEmpty()) {
Toast.makeText(context, context.getString(R.string.setupCallForwarding), Toast.LENGTH_LONG).show();
setCallForwarding(context, PhoneStateService.currentState, array[1]);
} else
} else {
if (PhoneStateService.currentState) {
setCallForwarding(context, true, "");
}
Toast.makeText(context, context.getString(R.string.NoNumber), Toast.LENGTH_SHORT).show();
}
}
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
if (Objects.equals(intent.getAction(), "android.intent.action.BOOT_COMPLETED")) {
Expand All @@ -53,9 +55,8 @@ public int getSavedSelectedSimId(Context context) {
return preferences.getInt("SELECTED_SIM_ID", -1); // -1 is a default value if the preference is not found
}


private void setCallForwarding(Context context, boolean cfi, String phoneNumber) {
Log.v(TAG, "setCallForwarding "+ cfi);
Log.v(TAG, "setCallForwarding " + cfi);
//if (DEBUG) Log.v(TAG, phoneNumber);
TelephonyManager manager = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);

Expand All @@ -75,7 +76,6 @@ private void setCallForwarding(Context context, boolean cfi, String phoneNumber)
}
}


Handler handler = new Handler();
TelephonyManager.UssdResponseCallback responseCallback = new TelephonyManager.UssdResponseCallback() {
@Override
Expand All @@ -96,17 +96,14 @@ public void onReceiveUssdResponseFailed(TelephonyManager telephonyManager, Strin
if (!cfi) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
String ussdRequest = "*21*" + phoneNumber + "#";

// Set the subscription ID for call forwarding
manager1 = manager.createForSubscriptionId(defaultSubId);

manager1.sendUssdRequest(ussdRequest, responseCallback, handler);
}
} else {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
// Set the subscription ID for call forwarding
manager1 = manager.createForSubscriptionId(defaultSubId);

manager1.sendUssdRequest("#21#", responseCallback, handler);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import android.Manifest;
import android.app.Activity;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -49,8 +50,6 @@

public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE_READ_PHONE_STATE_PERMISSION = 1;


final DatabaseHelper databaseHelper = new DatabaseHelper(this);
final String TAG = "Main";
//SQLiteDatabase database = databaseHelper.getWritableDatabase();
Expand All @@ -60,16 +59,38 @@ public class MainActivity extends AppCompatActivity {
RadioButton radioButton2;
RadioButton radioButton3;

@Override
protected void onRestart() {
checkPermission(this);
super.onRestart();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
activity = this;


checkPermission(this);
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.FOREGROUND_SERVICE}, 3);
//ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.FOREGROUND_SERVICE}, 3);

Button autoBootbutton = findViewById(R.id.autoBoot);
String manufacturer = android.os.Build.MANUFACTURER;
// Check if the manufacturer matches any of the specified brands
if ("xiaomi".equalsIgnoreCase(manufacturer) ||
"oppo".equalsIgnoreCase(manufacturer) ||
"vivo".equalsIgnoreCase(manufacturer) ||
"Letv".equalsIgnoreCase(manufacturer) ||
"Honor".equalsIgnoreCase(manufacturer)) {

// Set the button to be visible if the manufacturer matches any of the specified ones
autoBootbutton.setVisibility(View.VISIBLE);
autoBootbutton.setOnClickListener(view -> addAutoStartup());
} else {
// You can set the button to INVISIBLE or GONE if the manufacturer doesn't match
autoBootbutton.setVisibility(View.GONE); // or View.INVISIBLE
}

// Find the EditText views and ImageButton views
final EditText phoneNumber1EditText = findViewById(R.id.PhoneNumber1);
Expand All @@ -92,7 +113,6 @@ protected void onCreate(Bundle savedInstanceState) {
MultiSim(this);
updateMultiSimTxt();


// Set OnClickListeners on the ImageButton views
saveButton1.setOnClickListener(v -> saveSQLData(phoneNumber1EditText, 1));
saveButton2.setOnClickListener(v -> saveSQLData(phoneNumber2EditText, 2));
Expand Down Expand Up @@ -177,15 +197,13 @@ public void afterTextChanged(Editable editable) {
}
});


findViewById(R.id.button).setOnClickListener(view -> {
Intent intent = new Intent("de.kaiserdragon.callforwardingstatus.TOGGLE_CALL_FORWARDING");
intent.setClass(context, CallForwardingReceiver.class);
intent.putExtra("cfi", PhoneStateService.currentState);
context.sendBroadcast(intent);
});


RadioGroup radioGroup = findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
//group.clearCheck(); // Clear the previously selected radio button
Expand All @@ -200,6 +218,27 @@ public void afterTextChanged(Editable editable) {

}

private void addAutoStartup() {
try {
Intent intent = new Intent();
String manufacturer = android.os.Build.MANUFACTURER;
if ("xiaomi".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
} else if ("oppo".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
} else if ("vivo".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
} else if ("Letv".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity"));
} else if ("Honor".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
}
startActivity(intent);
} catch (Exception e) {
Log.e("exc", String.valueOf(e));
}
}

private void colorTextSaveStatus(EditText numberInput) {
numberInput.setTextColor(Color.RED);
}
Expand All @@ -217,10 +256,12 @@ public void MultiSim(Context context) {
if (subscriptionManager != null) {
if (context.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
List<SubscriptionInfo> subscriptionList = subscriptionManager.getActiveSubscriptionInfoList();
assert subscriptionList != null;
if (DEBUG) Log.i(TAG, String.valueOf(subscriptionList.size()));
if (subscriptionList.size() <= 1) {
findViewById(R.id.multisim_button).setVisibility(View.GONE);
} else findViewById(R.id.multisim_button).setOnClickListener(view -> showSimSelectionPopup(this));
if (subscriptionList.size() > 1) {
findViewById(R.id.multisim_button).setVisibility(View.VISIBLE);
} else
findViewById(R.id.multisim_button).setOnClickListener(view -> showSimSelectionPopup(this));
}
}
}
Expand Down Expand Up @@ -311,6 +352,7 @@ private void saveSQLData(EditText numberInput, int row) {
int color = ContextCompat.getColor(this, typedValue.resourceId);
numberInput.setTextColor(color);
if (isFirstEntry(database)) {
databaseHelper.changeSelected(String.valueOf(row));
if (Objects.equals(row, 1)) radioButton1.setChecked(true);
if (Objects.equals(row, 2)) radioButton2.setChecked(true);
if (Objects.equals(row, 3)) radioButton3.setChecked(true);
Expand All @@ -335,10 +377,10 @@ public boolean isFirstEntry(SQLiteDatabase db) {
try (Cursor cursor = db.query("phone_numbers", columns, null, null, null, null, null)) {
if (cursor.moveToFirst()) {
int count = cursor.getInt(0);
Log.i(TAG, String.valueOf(count));
return count == 1;
}
}

return false; // Return false by default if an exception occurs or cursor is null
}

Expand Down Expand Up @@ -371,11 +413,8 @@ public void checkPermission(Activity activity) {
Log.i(TAG, "Try Again");
// Request the permission
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_CODE_READ_PHONE_STATE_PERMISSION);
return;
}
// return;
}
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
} else {
//Permission already granted start service
Intent serviceIntent = new Intent(context, PhoneStateService.class);
context.startForegroundService(serviceIntent);
Expand All @@ -391,7 +430,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission was granted, you can do the required task
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CALL_PHONE}, 2);
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.FOREGROUND_SERVICE}, 3);
//ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.FOREGROUND_SERVICE}, 3);
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_PHONE_STATE)) {
// Show a dialog explaining the need for the permission
Expand Down Expand Up @@ -421,37 +460,11 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
.create()
.show();
return;
}/*
// Permission was denied, you can show a message to the user
new AlertDialog.Builder(this)
.setTitle("Permission needed")
.setMessage("This permission is needed to read the phone state and update the widget accordingly.")
.setPositiveButton("Ok", (dialog, which) -> {
// Redirect the user to the app's settings page
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
})
//.setNegativeButton("Cancel", (dialog, which) -> dialog.dismiss())
.create()
.show();
}*/
}
}
}
if (requestCode == 2) {

// Check if the permission was granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S_V2) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 4);
} else {
Intent serviceIntent = new Intent(context, PhoneStateService.class);
context.startForegroundService(serviceIntent);
}
}
}
if (requestCode == 4) {
// Check if the permission was granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent serviceIntent = new Intent(context, PhoneStateService.class);
Expand Down
37 changes: 24 additions & 13 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
android:fitsSystemWindows="true"
tools:context="de.kaiserdragon.callforwardingstatus.MainActivity">

<TextView
android:id="@+id/MainText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:text="@string/DescriptionMain"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/multisim_button"
android:layout_width="wrap_content"
Expand All @@ -18,36 +32,33 @@
android:paddingBottom="10dp"
android:text="@string/select_sim"
android:textSize="20sp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.534"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/MainText" />

<TextView
android:id="@+id/MainText"
<Button
android:id="@+id/autoBoot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:text="@string/DescriptionMain"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:text="@string/allow_boot"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/multisim_button" />

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginTop="50dp"
android:foregroundGravity="center"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/multisim_button">
app:layout_constraintTop_toBottomOf="@+id/autoBoot">

<RadioGroup
android:id="@+id/radioGroup"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@
<string name="applyColor">Farbe anwenden</string>
<string name="select_sim">Wähle eine SIM</string>
<string name="notification_channel_name">Rufumleitung</string>
<string name="allow_boot">Erlaube Autostart</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@
<string name="applyColor">Apply Color</string>
<string name="select_sim">Select Simcard</string>
<string name="notification_channel_name">CallForwarding</string>
<string name="allow_boot">Allow Start on Boot</string>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext {
agp_version = '8.7.2'
agp_version = '8.7.3'
}
repositories {
google()
Expand Down

0 comments on commit 21f69b1

Please sign in to comment.