Skip to content

Commit

Permalink
Add New Tor identity button
Browse files Browse the repository at this point in the history
  • Loading branch information
Gedsh committed Jul 29, 2020
1 parent cb0c74b commit 12adbc4
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

Expand All @@ -72,6 +75,7 @@
import pan.alexander.tordnscrypt.main_fragment.ViewPagerAdapter;
import pan.alexander.tordnscrypt.modules.ModulesAux;
import pan.alexander.tordnscrypt.modules.ModulesKiller;
import pan.alexander.tordnscrypt.modules.ModulesRestarter;
import pan.alexander.tordnscrypt.modules.ModulesService;
import pan.alexander.tordnscrypt.modules.ModulesStatus;
import pan.alexander.tordnscrypt.settings.PathVars;
Expand All @@ -88,6 +92,7 @@
import static pan.alexander.tordnscrypt.TopFragment.appVersion;
import static pan.alexander.tordnscrypt.assistance.AccelerateDevelop.accelerated;
import static pan.alexander.tordnscrypt.utils.RootExecService.LOG_TAG;
import static pan.alexander.tordnscrypt.utils.enums.ModuleState.RUNNING;
import static pan.alexander.tordnscrypt.utils.enums.ModuleState.STOPPED;
import static pan.alexander.tordnscrypt.utils.enums.OperationMode.PROXY_MODE;
import static pan.alexander.tordnscrypt.utils.enums.OperationMode.ROOT_MODE;
Expand All @@ -114,7 +119,11 @@ public class MainActivity extends LangAppCompatActivity
private ModulesStatus modulesStatus;
private ViewPager viewPager;
private static int viewPagerPosition = 0;
private MenuItem newIdentityMenuItem;
private ImageView animatingImage;
private RotateAnimation rotateAnimation;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {

Expand Down Expand Up @@ -340,6 +349,8 @@ public boolean onPrepareOptionsMenu(Menu menu) {
switchApIcon(menu);
}

showNewTorIdentityIcon(menu);

return super.onPrepareOptionsMenu(menu);
}

Expand Down Expand Up @@ -488,6 +499,31 @@ private void switchChildLockIcon(Menu menu) {
}
}

private void showNewTorIdentityIcon(Menu menu) {
newIdentityMenuItem = menu.findItem(R.id.item_new_identity);

if (newIdentityMenuItem == null || modulesStatus == null) {
return;
}

if (modulesStatus.getTorState() == RUNNING) {
newIdentityMenuItem.setVisible(true);
} else {
newIdentityMenuItem.setVisible(false);
}
}

public void showNewTorIdentityIcon(boolean show) {

if (newIdentityMenuItem == null || modulesStatus == null) {
return;
}

newIdentityMenuItem.setVisible(show);

invalidateOptionsMenu();
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
Expand All @@ -511,6 +547,9 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
case R.id.item_root:
showInfoAboutRoot();
break;
case R.id.item_new_identity:
newTorIdentity();
break;
case R.id.menu_root_mode:
switchToRootMode(item);
break;
Expand Down Expand Up @@ -542,6 +581,36 @@ private void switchHotspot() {
}
}

@SuppressLint("InflateParams")
private void newTorIdentity() {
if (modulesStatus != null && newIdentityMenuItem != null && modulesStatus.getTorState() == RUNNING) {

if (rotateAnimation == null || animatingImage == null) {
rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration((long) 1000);
rotateAnimation.setRepeatCount(3);

LayoutInflater inflater = getLayoutInflater();
animatingImage = (ImageView) inflater.inflate(R.layout.icon_image_new_tor_identity, null);
}

if (rotateAnimation != null && animatingImage != null) {
animatingImage.startAnimation(rotateAnimation);
newIdentityMenuItem.setActionView(animatingImage);
}

ModulesRestarter.restartTor(this);

new Handler().postDelayed(() -> {
if (!isFinishing() && newIdentityMenuItem != null) {
Toast.makeText(this, this.getText(R.string.toast_new_tor_identity), Toast.LENGTH_SHORT).show();
newIdentityMenuItem.getActionView().clearAnimation();
newIdentityMenuItem.setActionView(null);
}
}, 3000);
}
}

private void switchToRootMode(MenuItem item) {
item.setChecked(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ public void setTorStopped(Context context) {
if (context != null) {
new PrefManager(context).setBoolPref("Tor Ready", false);
}

showNewTorIdentityIcon(false);
}

@Override
Expand Down Expand Up @@ -456,6 +458,8 @@ private void torStartedSuccessfully(Context context, String lastLines) {

view.setTorProgressBarProgress(0);

showNewTorIdentityIcon(true);

boolean torReady = new PrefManager(context).getBoolPref("Tor Ready");

if (!torReady) {
Expand Down Expand Up @@ -790,4 +794,11 @@ private void checkInternetAvailable() {
public void torLogAutoScrollingAllowed(boolean allowed) {
torLogAutoScroll = allowed;
}

private void showNewTorIdentityIcon(boolean show) {
if (view != null && view.getFragmentActivity() != null && view.getFragmentActivity() instanceof MainActivity) {
MainActivity mainActivity = (MainActivity) view.getFragmentActivity();
mainActivity.showNewTorIdentityIcon(show);
}
}
}
5 changes: 5 additions & 0 deletions tordnscrypt/src/main/res/drawable/ic_baseline_refresh_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_refresh_24"
style="@style/Widget.AppCompat.ActionButton"
android:contentDescription="@string/btn_new_tor_identity" />
14 changes: 10 additions & 4 deletions tordnscrypt/src/main/res/menu/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,29 @@
android:orderInCategory="3"
android:title="@string/action_mode_child_lock"
app:showAsAction="always" />
<item
android:id="@+id/item_new_identity"
android:icon="@drawable/ic_baseline_refresh_24"
android:orderInCategory="4"
android:title="@string/btn_new_tor_identity"
app:showAsAction="always" />
<group
android:checkableBehavior="single"
android:menuCategory="container"
android:orderInCategory="4">
android:orderInCategory="5">
<item
android:id="@+id/menu_root_mode"
android:orderInCategory="5"
android:orderInCategory="6"
android:title="@string/menu_use_root"
app:showAsAction="never" />
<item
android:id="@+id/menu_vpn_mode"
android:orderInCategory="6"
android:orderInCategory="7"
android:title="@string/menu_use_vpn"
app:showAsAction="never" />
<item
android:id="@+id/menu_proxies_mode"
android:orderInCategory="7"
android:orderInCategory="8"
android:title="@string/menu_use_proxies"
app:showAsAction="never" />
</group>
Expand Down
2 changes: 2 additions & 0 deletions tordnscrypt/src/main/res/values-ru-rRU/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -441,4 +441,6 @@
<string name="pref_common_compatibility_mode">Режим совместимости</string>
<string name="pref_common_compatibility_mode_summ">Включите если у Вас кастомная прошивка, и интернет пропадает при нажатии кнопки START</string>
<string name="dialog_send_crash_report">Обнаружен отчет аварийного завершения приложения. Хотите его отправить разработчику, чтобы сделать InviZible лучше?</string>
<string name="btn_new_tor_identity">Новый идентификатор Tor</string>
<string name="toast_new_tor_identity">Идентификатор Tor изменился</string>
</resources>
2 changes: 2 additions & 0 deletions tordnscrypt/src/main/res/values-ru-rUA/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,6 @@
<string name="pref_common_compatibility_mode">Режим совместимости</string>
<string name="pref_common_compatibility_mode_summ">Включите если у Вас кастомная прошивка, и интернет пропадает при нажатии кнопки START</string>
<string name="dialog_send_crash_report">Обнаружен отчет аварийного завершения приложения. Хотите его отправить разработчику, чтобы сделать InviZible лучше?</string>
<string name="btn_new_tor_identity">Новый идентификатор Tor</string>
<string name="toast_new_tor_identity">Идентификатор Tor изменился</string>
</resources>
2 changes: 2 additions & 0 deletions tordnscrypt/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -683,5 +683,7 @@
<string name="notification_channel_root">Root commands notification</string>

<string name="dialog_send_crash_report">A crash report has been discovered. Want to send it to the developer to make InviZible better?</string>
<string name="btn_new_tor_identity">New Tor identity</string>
<string name="toast_new_tor_identity">Tor identity has changed</string>

</resources>

0 comments on commit 12adbc4

Please sign in to comment.