Skip to content

Commit

Permalink
feat: remove deprecations and update error activity layout
Browse files Browse the repository at this point in the history
  • Loading branch information
edusperoni committed Jul 8, 2024
1 parent 1cf7105 commit 34eaae5
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 40 deletions.
1 change: 1 addition & 0 deletions test-app/runtime/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ dependencies {
implementation "androidx.appcompat:appcompat:$androidXAppCompatVersion"
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
implementation 'androidx.preference:preference:1.2.1'
}


Expand Down
37 changes: 19 additions & 18 deletions test-app/runtime/src/debug/java/com/tns/ErrorReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

Expand All @@ -59,7 +60,7 @@ class ErrorReport implements TabLayout.OnTabSelectedListener {
private static AppCompatActivity activity;

private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPager2 viewPager;
private Context context;

private static String exceptionMsg;
Expand Down Expand Up @@ -279,12 +280,12 @@ void buildUI() {
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
int pagerId = this.context.getResources().getIdentifier("pager", "id", this.context.getPackageName());

viewPager = (ViewPager) activity.findViewById(pagerId);
viewPager = (ViewPager2) activity.findViewById(pagerId);

Pager adapter = new Pager(activity.getSupportFragmentManager(), tabLayout.getTabCount());
Pager adapter = new Pager(activity, tabLayout.getTabCount());

viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

Expand Down Expand Up @@ -332,18 +333,18 @@ public void onTabReselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

private class Pager extends FragmentStatePagerAdapter {
private class Pager extends FragmentStateAdapter {

int tabCount;

@SuppressWarnings("deprecation")
public Pager(FragmentManager fm, int tabCount) {
public Pager(FragmentActivity fm, int tabCount) {
super(fm);
this.tabCount = tabCount;
}

@Override
public Fragment getItem(int position) {
public Fragment createFragment(int position) {
switch (position) {
case 0:
return new ExceptionTab();
Expand All @@ -355,7 +356,7 @@ public Fragment getItem(int position) {
}

@Override
public int getCount() {
public int getItemCount() {
return tabCount;
}
}
Expand Down Expand Up @@ -410,14 +411,14 @@ public static void restartApp(Context context) {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int exceptionTabId = container.getContext().getResources().getIdentifier("exception_tab", "layout", container.getContext().getPackageName());
int exceptionTabId = getContext().getResources().getIdentifier("exception_tab", "layout", getContext().getPackageName());
View view = inflater.inflate(exceptionTabId, container, false);

int errorExceptionViewId = activity.getResources().getIdentifier("errorException", "id", activity.getPackageName());
TextView errorExceptionView = (TextView) activity.findViewById(errorExceptionViewId);
errorExceptionView.setMovementMethod(new ScrollingMovementMethod());

int errorStackTraceViewId = container.getContext().getResources().getIdentifier("errorStacktrace", "id", container.getContext().getPackageName());
int errorStackTraceViewId = getContext().getResources().getIdentifier("errorStacktrace", "id", getContext().getPackageName());
TextView errorStackTraceView = (TextView) view.findViewById(errorStackTraceViewId);

String[] exceptionParts = exceptionMsg.split("StackTrace:");
Expand All @@ -438,10 +439,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
errorStackTraceView.setMovementMethod(LinkMovementMethod.getInstance());
errorStackTraceView.setEnabled(true);

int btnCopyExceptionId = container.getContext().getResources().getIdentifier("btnCopyException", "id", container.getContext().getPackageName());
int btnCopyExceptionId = getContext().getResources().getIdentifier("btnCopyException", "id", getContext().getPackageName());
Button copyToClipboard = (Button) view.findViewById(btnCopyExceptionId);

int btnRestartAppId = container.getContext().getResources().getIdentifier("btnRestartApp", "id", container.getContext().getPackageName());
int btnRestartAppId = getContext().getResources().getIdentifier("btnRestartApp", "id", getContext().getPackageName());
Button restartApp = (Button) view.findViewById(btnRestartAppId);
restartApp.setOnClickListener(v -> {
restartApp(getContext().getApplicationContext());
Expand All @@ -459,18 +460,18 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
public static class LogcatTab extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int logcatTabId = container.getContext().getResources().getIdentifier("logcat_tab", "layout", container.getContext().getPackageName());
int logcatTabId = getContext().getResources().getIdentifier("logcat_tab", "layout", getContext().getPackageName());
View view = inflater.inflate(logcatTabId, container, false);

int textViewId = container.getContext().getResources().getIdentifier("logcatMsg", "id", container.getContext().getPackageName());
int textViewId = getContext().getResources().getIdentifier("logcatMsg", "id", getContext().getPackageName());
TextView txtlogcatMsg = (TextView) view.findViewById(textViewId);
txtlogcatMsg.setText(logcatMsg);

txtlogcatMsg.setMovementMethod(new ScrollingMovementMethod());

final String logName = "Log-" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());

int btnCopyLogcatId = container.getContext().getResources().getIdentifier("btnCopyLogcat", "id", container.getContext().getPackageName());
int btnCopyLogcatId = getContext().getResources().getIdentifier("btnCopyLogcat", "id", getContext().getPackageName());
Button copyToClipboard = (Button) view.findViewById(btnCopyLogcatId);
copyToClipboard.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down Expand Up @@ -498,7 +499,7 @@ public void onClick(View v) {
} catch (Exception e) {
String err = "Could not write logcat report to sdcard. Make sure you have allowed access to external storage!";
Toast.makeText(activity, err, Toast.LENGTH_LONG).show();
if (Util.isDebuggableApp(container.getContext())) {
if (Util.isDebuggableApp(getContext())) {
e.printStackTrace();
}
}
Expand Down
4 changes: 2 additions & 2 deletions test-app/runtime/src/debug/res/layout/error_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize" />

<androidx.viewpager.widget.ViewPager
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/toolbar"
android:scrollbarAlwaysDrawVerticalTrack="false">

</androidx.viewpager.widget.ViewPager>
</androidx.viewpager2.widget.ViewPager2>

<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
Expand Down
22 changes: 12 additions & 10 deletions test-app/runtime/src/debug/res/layout/exception_tab.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,26 @@

<Button
android:id="@+id/btnRestartApp"
android:text="Restart APP"
android:textAlignment="center"
android:textColor="@android:color/white"
android:background="@drawable/button_accented"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:layout_marginRight="10dp" />
android:background="@drawable/button_accented"
android:foreground="?android:attr/selectableItemBackground"
android:text="Restart APP"
android:textAlignment="center"
android:textColor="@android:color/white" />

<Button
android:id="@+id/btnCopyException"
android:textColor="@android:color/white"
android:text="Copy"
android:background="@drawable/button"
android:textAlignment="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
android:layout_weight="1"
android:background="@drawable/button"
android:foreground="?android:attr/selectableItemBackground"
android:text="Copy"
android:textAlignment="center"
android:textColor="@android:color/white" />


</LinearLayout>
Expand Down
17 changes: 9 additions & 8 deletions test-app/runtime/src/debug/res/layout/logcat_tab.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@

<Button
android:id="@+id/btnCopyLogcat"
android:layout_height="wrap_content"
android:text="Copy to sdcard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/button"
android:textColor="@android:color/white"
android:textAlignment="center"
android:foreground="?android:attr/selectableItemBackground"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Copy to sdcard"
android:textAlignment="center"
android:textColor="@android:color/white"
tools:layout_width="match_parent" />

</LinearLayout>
Expand Down
2 changes: 1 addition & 1 deletion test-app/runtime/src/main/java/com/tns/RuntimeHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.preference.PreferenceManager;
import androidx.preference.PreferenceManager;
import android.util.Log;

import java.io.File;
Expand Down
2 changes: 1 addition & 1 deletion test-app/runtime/src/main/java/com/tns/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static boolean runPlugin(Logger logger, Context context) {

try {
Class<?> liveSyncPluginClass = Class.forName(pluginClassName);
Plugin p = (Plugin) liveSyncPluginClass.newInstance();
Plugin p = (Plugin) liveSyncPluginClass.getDeclaredConstructor().newInstance();
success = p.execute(context);
} catch (Exception e) {
if (Util.isDebuggableApp(context) && logger.isEnabled()) {
Expand Down

0 comments on commit 34eaae5

Please sign in to comment.