diff options
author | Arne Schwabe <arne@rfc2549.org> | 2024-07-29 17:21:39 +0200 |
---|---|---|
committer | Arne Schwabe <arne@rfc2549.org> | 2024-07-29 17:38:25 +0200 |
commit | c2d625eef66c2ede30f0e5c8d03ca279975eefc8 (patch) | |
tree | d5b4fc922e8136b546f3fbf5355041069f32d1e2 /main/src/ui/java/de | |
parent | 291acb38ddf8cf8c8f7dab2f1aad6bb860ae6109 (diff) |
Implement enough of edge-to-edge to work well on Android V
Diffstat (limited to 'main/src/ui/java/de')
15 files changed, 216 insertions, 103 deletions
diff --git a/main/src/ui/java/de/blinkt/openvpn/activities/BaseActivity.java b/main/src/ui/java/de/blinkt/openvpn/activities/BaseActivity.java deleted file mode 100644 index cca8b155..00000000 --- a/main/src/ui/java/de/blinkt/openvpn/activities/BaseActivity.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2012-2015 Arne Schwabe - * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt - */ - -package de.blinkt.openvpn.activities; - -import android.app.UiModeManager; -import android.content.Context; -import android.content.res.Configuration; -import android.os.Bundle; -import android.view.Window; - -import androidx.annotation.NonNull; -import androidx.appcompat.app.AppCompatActivity; - -import de.blinkt.openvpn.core.LocaleHelper; - -public abstract class BaseActivity extends AppCompatActivity { - boolean isAndroidTV() { - final UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE); - if (uiModeManager == null) - return false; - return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION; - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - if (isAndroidTV()) { - requestWindowFeature(Window.FEATURE_OPTIONS_PANEL); - } - super.onCreate(savedInstanceState); - } - - @Override - protected void attachBaseContext(Context base) { - super.attachBaseContext(LocaleHelper.updateResources(base)); - } - - @Override - public void onConfigurationChanged(@NonNull Configuration newConfig) { - super.onConfigurationChanged(newConfig); - LocaleHelper.onConfigurationChange(this); - } -} diff --git a/main/src/ui/java/de/blinkt/openvpn/activities/BaseActivity.kt b/main/src/ui/java/de/blinkt/openvpn/activities/BaseActivity.kt new file mode 100644 index 00000000..9e6dd462 --- /dev/null +++ b/main/src/ui/java/de/blinkt/openvpn/activities/BaseActivity.kt @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2012-2015 Arne Schwabe + * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt + */ +package de.blinkt.openvpn.activities + +import android.app.UiModeManager +import android.content.Context +import android.content.res.Configuration +import android.os.Bundle +import android.view.View +import android.view.ViewGroup +import android.view.Window +import androidx.activity.SystemBarStyle +import androidx.activity.enableEdgeToEdge +import androidx.appcompat.app.AppCompatActivity +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat +import androidx.core.view.updateLayoutParams +import androidx.core.view.updatePadding +import de.blinkt.openvpn.R +import de.blinkt.openvpn.core.LocaleHelper + +abstract class BaseActivity : AppCompatActivity() { + val isAndroidTV: Boolean + get() { + val uiModeManager = getSystemService(UI_MODE_SERVICE) as UiModeManager + return uiModeManager.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION + } + + override fun onCreate(savedInstanceState: Bundle?) { + if (isAndroidTV) { + requestWindowFeature(Window.FEATURE_OPTIONS_PANEL) + } + this.enableEdgeToEdge(SystemBarStyle.dark(R.color.primary_dark)) + super.onCreate(savedInstanceState) + } + + fun setUpEdgeEdgeInsetsListener( + rootView: View, + contentViewId: Int = R.id.root_linear_layout, + setupBottom: Boolean = true + ) { + val contentView = rootView.findViewById<View>(contentViewId) + + ViewCompat.setOnApplyWindowInsetsListener(contentView) { v, windowInsets -> + val insets = + windowInsets.getInsets(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout()) + val statusbarbg = findViewById<View>(R.id.statusbar_background); + + val statusBarInsets = windowInsets.getInsets(WindowInsetsCompat.Type.statusBars()) + + statusbarbg.layoutParams.height = statusBarInsets.top + + + v.updateLayoutParams<ViewGroup.MarginLayoutParams> { + topMargin = insets.top + } + + v.updatePadding( + left = insets.left, + right = insets.right, + ) + if (setupBottom) { + v.updatePadding(bottom = insets.bottom) + WindowInsetsCompat.CONSUMED + } else { + windowInsets + } + } + } + + override fun attachBaseContext(base: Context) { + super.attachBaseContext(LocaleHelper.updateResources(base)) + } + + override fun onConfigurationChanged(newConfig: Configuration) { + super.onConfigurationChanged(newConfig) + LocaleHelper.onConfigurationChange(this) + } +} diff --git a/main/src/ui/java/de/blinkt/openvpn/activities/ConfigConverter.kt b/main/src/ui/java/de/blinkt/openvpn/activities/ConfigConverter.kt index b85eaa26..a80afcab 100644 --- a/main/src/ui/java/de/blinkt/openvpn/activities/ConfigConverter.kt +++ b/main/src/ui/java/de/blinkt/openvpn/activities/ConfigConverter.kt @@ -618,7 +618,9 @@ class ConfigConverter : BaseActivity(), FileSelectCallback, View.OnClickListener override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.config_converter) + val v = layoutInflater.inflate(R.layout.config_converter, null) + setUpEdgeEdgeInsetsListener(v, R.id.root_layout_config_converter) + setContentView(v) val fab_button = findViewById<ImageButton?>(R.id.fab_save) if (fab_button != null) { diff --git a/main/src/ui/java/de/blinkt/openvpn/activities/InternalWebView.kt b/main/src/ui/java/de/blinkt/openvpn/activities/InternalWebView.kt index e46356e9..fec69768 100644 --- a/main/src/ui/java/de/blinkt/openvpn/activities/InternalWebView.kt +++ b/main/src/ui/java/de/blinkt/openvpn/activities/InternalWebView.kt @@ -22,7 +22,7 @@ import de.blinkt.openvpn.R import de.blinkt.openvpn.VpnProfile import org.json.JSONObject -class InternalWebView : AppCompatActivity() { +class InternalWebView : BaseActivity() { lateinit var webView: WebView lateinit var urlTextView: TextView @@ -32,6 +32,8 @@ class InternalWebView : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.webview_internal) + setUpEdgeEdgeInsetsListener(getWindow().getDecorView().getRootView(), R.id.container) + webView = findViewById(R.id.internal_webview) urlTextView = findViewById(R.id.url_textview) diff --git a/main/src/ui/java/de/blinkt/openvpn/activities/LogWindow.java b/main/src/ui/java/de/blinkt/openvpn/activities/LogWindow.java deleted file mode 100644 index 5277a25d..00000000 --- a/main/src/ui/java/de/blinkt/openvpn/activities/LogWindow.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2012-2016 Arne Schwabe - * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt - */ - -package de.blinkt.openvpn.activities; - -import android.app.Activity; -import android.os.Bundle; -import android.view.MenuItem; - -import de.blinkt.openvpn.R; -import de.blinkt.openvpn.fragments.LogFragment; - -/** - * Created by arne on 13.10.13. - */ -public class LogWindow extends BaseActivity { - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.log_window); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - - if (savedInstanceState == null) { - getSupportFragmentManager().beginTransaction() - .add(R.id.container, new LogFragment()) - .commit(); - } - - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - return super.onOptionsItemSelected(item); - } -} diff --git a/main/src/ui/java/de/blinkt/openvpn/activities/LogWindow.kt b/main/src/ui/java/de/blinkt/openvpn/activities/LogWindow.kt new file mode 100644 index 00000000..afc18a03 --- /dev/null +++ b/main/src/ui/java/de/blinkt/openvpn/activities/LogWindow.kt @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2012-2016 Arne Schwabe + * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt + */ +package de.blinkt.openvpn.activities + +import android.os.Bundle +import android.view.MenuItem +import de.blinkt.openvpn.R +import de.blinkt.openvpn.fragments.LogFragment + +/** + * Created by arne on 13.10.13.setUpEdgeEdgeStuff + */ +class LogWindow : BaseActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.log_window) + supportActionBar?.setDisplayHomeAsUpEnabled(true) + + if (savedInstanceState == null) { + supportFragmentManager.beginTransaction() + .add(R.id.container, LogFragment()) + .commit() + } + + setUpEdgeEdgeInsetsListener(getWindow().getDecorView().getRootView(), R.id.container) + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + return super.onOptionsItemSelected(item) + } +} diff --git a/main/src/ui/java/de/blinkt/openvpn/activities/MainActivity.kt b/main/src/ui/java/de/blinkt/openvpn/activities/MainActivity.kt index 49778b88..a15de114 100644 --- a/main/src/ui/java/de/blinkt/openvpn/activities/MainActivity.kt +++ b/main/src/ui/java/de/blinkt/openvpn/activities/MainActivity.kt @@ -10,7 +10,6 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.widget.Toast -import androidx.viewpager.widget.ViewPager import androidx.viewpager2.widget.ViewPager2 import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayoutMediator @@ -22,13 +21,14 @@ import de.blinkt.openvpn.views.ScreenSlidePagerAdapter class MainActivity : BaseActivity() { private lateinit var mPager: ViewPager2 private lateinit var mPagerAdapter: ScreenSlidePagerAdapter + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.main_activity) + val view = layoutInflater.inflate(R.layout.main_activity, null) // Instantiate a ViewPager and a PagerAdapter. - mPager = findViewById(R.id.pager) - val tablayout: TabLayout = findViewById(R.id.tab_layout) + mPager = view.findViewById(R.id.pager) + val tablayout: TabLayout = view.findViewById(R.id.tab_layout) mPagerAdapter = ScreenSlidePagerAdapter(supportFragmentManager, lifecycle, this) @@ -49,8 +49,11 @@ class MainActivity : BaseActivity() { tab.text = mPagerAdapter.getPageTitle(position) }.attach() + setUpEdgeEdgeInsetsListener(view, R.id.root_linear_layout) + setContentView(view) } + private fun disableToolbarElevation() { supportActionBar?.elevation = 0f } diff --git a/main/src/ui/java/de/blinkt/openvpn/activities/OpenSSLSpeed.kt b/main/src/ui/java/de/blinkt/openvpn/activities/OpenSSLSpeed.kt index 324cd881..e7c6c2db 100644 --- a/main/src/ui/java/de/blinkt/openvpn/activities/OpenSSLSpeed.kt +++ b/main/src/ui/java/de/blinkt/openvpn/activities/OpenSSLSpeed.kt @@ -105,6 +105,7 @@ class OpenSSLSpeed : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.openssl_speed) + setUpEdgeEdgeInsetsListener(getWindow().getDecorView().getRootView(), R.id.speed_root) supportActionBar!!.setDisplayHomeAsUpEnabled(true) findViewById<View>(R.id.testSpecific).setOnClickListener { _ -> runAlgorithms(mCipher.text.toString()) } diff --git a/main/src/ui/java/de/blinkt/openvpn/activities/VPNPreferences.kt b/main/src/ui/java/de/blinkt/openvpn/activities/VPNPreferences.kt index fd9fd43e..0dd61748 100644 --- a/main/src/ui/java/de/blinkt/openvpn/activities/VPNPreferences.kt +++ b/main/src/ui/java/de/blinkt/openvpn/activities/VPNPreferences.kt @@ -104,13 +104,14 @@ class VPNPreferences : BaseActivity() { title = getString(R.string.edit_profile_title, mProfile!!.name) - setContentView(R.layout.main_activity) + val rootview = layoutInflater.inflate(R.layout.main_activity, null) + setUpEdgeEdgeInsetsListener(rootview, R.id.root_linear_layout) disableToolbarElevation() // Instantiate a ViewPager and a PagerAdapter. - mPager = findViewById(R.id.pager) - val tablayout: TabLayout = findViewById(R.id.tab_layout) + mPager = rootview.findViewById(R.id.pager) + val tablayout: TabLayout = rootview.findViewById(R.id.tab_layout) mPagerAdapter = ScreenSlidePagerAdapter(supportFragmentManager, lifecycle, this) @@ -143,6 +144,8 @@ class VPNPreferences : BaseActivity() { TabLayoutMediator(tablayout, mPager) { tab, position -> tab.text = mPagerAdapter.getPageTitle(position) }.attach() + + setContentView(rootview) } diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/AboutFragment.java b/main/src/ui/java/de/blinkt/openvpn/fragments/AboutFragment.java index de6c83d8..15375a3f 100644 --- a/main/src/ui/java/de/blinkt/openvpn/fragments/AboutFragment.java +++ b/main/src/ui/java/de/blinkt/openvpn/fragments/AboutFragment.java @@ -27,6 +27,9 @@ import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import androidx.core.graphics.Insets; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; import androidx.fragment.app.Fragment; import com.android.vending.billing.IInAppBillingService; @@ -108,6 +111,16 @@ public class AboutFragment extends Fragment implements View.OnClickListener { TextView wv = (TextView) v.findViewById(R.id.full_licenses); wv.setText(Html.fromHtml(readHtmlFromAssets())); + + + + ViewCompat.setOnApplyWindowInsetsListener(v, (view, windowInsets) -> + { + Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout()); + view.setPadding(view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(), insets.bottom); + return WindowInsetsCompat.CONSUMED; + } + ); return v; } diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/FaqFragment.java b/main/src/ui/java/de/blinkt/openvpn/fragments/FaqFragment.java index dcdfd5e3..2863b242 100644 --- a/main/src/ui/java/de/blinkt/openvpn/fragments/FaqFragment.java +++ b/main/src/ui/java/de/blinkt/openvpn/fragments/FaqFragment.java @@ -9,6 +9,9 @@ import android.content.Context; import android.os.Build; import android.os.Bundle; +import androidx.core.graphics.Insets; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.StaggeredGridLayoutManager; @@ -196,9 +199,13 @@ public class FaqFragment extends Fragment { mRecyclerView.setAdapter(new FaqViewAdapter(getActivity(), getFAQEntries())); + Utils.applyInsetListener(v); + return v; } + + private FAQEntry[] getFAQEntries() { Vector<FAQEntry> faqItems = new Vector<>(); diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/LogFragment.java b/main/src/ui/java/de/blinkt/openvpn/fragments/LogFragment.java index 9ce65316..4a7cf735 100644 --- a/main/src/ui/java/de/blinkt/openvpn/fragments/LogFragment.java +++ b/main/src/ui/java/de/blinkt/openvpn/fragments/LogFragment.java @@ -9,11 +9,9 @@ import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.ObjectAnimator; import android.app.Activity; -import android.app.PendingIntent; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.content.res.Resources; import android.database.DataSetObserver; @@ -38,8 +36,6 @@ import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.AdapterView.OnItemLongClickListener; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.LinearLayout; @@ -631,16 +627,26 @@ public class LogFragment extends ListFragment implements StateListener, SeekBar. }); if (mShowOptionsLayout) mOptionsLayout.setVisibility(View.VISIBLE); + +// ViewCompat.setOnApplyWindowInsetsListener(v, (view, windowInsets) -> +// { +// Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout()); +// view.setPadding(view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(), insets.bottom); +// return WindowInsetsCompat.CONSUMED; +// } +// ); + Utils.applyInsetListener(v); + return v; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - // Scroll to the end of the list end - //getListView().setSelection(getListView().getAdapter().getCount()-1); } + + @Override public void onAttach(@NonNull Context activity) { super.onAttach(activity); diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/Settings_Connections.kt b/main/src/ui/java/de/blinkt/openvpn/fragments/Settings_Connections.kt index 8ed9d9ef..b06c3368 100644 --- a/main/src/ui/java/de/blinkt/openvpn/fragments/Settings_Connections.kt +++ b/main/src/ui/java/de/blinkt/openvpn/fragments/Settings_Connections.kt @@ -40,12 +40,8 @@ class Settings_Connections : Settings_Fragment(), View.OnClickListener { val v = inflater.inflate(R.layout.connections, container, false) mWarning = v.findViewById<View>(R.id.noserver_active_warning) as TextView mRecyclerView = v.findViewById<View>(R.id.connection_recycler_view) as RecyclerView - val dpwidth = (container!!.width / resources.displayMetrics.density).toInt() - var columns = dpwidth / 290 - columns = 1.coerceAtLeast(columns) mConnectionsAdapter = ConnectionsAdapter(activity, this, mProfile) - //mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(columns, StaggeredGridLayoutManager.VERTICAL)); mRecyclerView.setHasFixedSize(true) mRecyclerView.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false) diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/Utils.kt b/main/src/ui/java/de/blinkt/openvpn/fragments/Utils.kt index 55056424..e41dc337 100644 --- a/main/src/ui/java/de/blinkt/openvpn/fragments/Utils.kt +++ b/main/src/ui/java/de/blinkt/openvpn/fragments/Utils.kt @@ -13,12 +13,14 @@ import android.os.Build import android.provider.OpenableColumns import android.text.SpannableString import android.text.SpannableStringBuilder -import android.text.TextUtils import android.text.style.ForegroundColorSpan import android.util.Base64 +import android.view.View import android.webkit.MimeTypeMap +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat +import androidx.core.view.updatePadding import de.blinkt.openvpn.R -import kotlin.Throws import de.blinkt.openvpn.VpnProfile import de.blinkt.openvpn.core.Preferences import java.io.ByteArrayOutputStream @@ -319,4 +321,37 @@ object Utils { } } + @JvmStatic + fun applyInsetListener(v:View) + { + ViewCompat.setOnApplyWindowInsetsListener( + v + ) { view: View, windowInsets: WindowInsetsCompat -> + val insets = + windowInsets.getInsets(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout()) + view.updatePadding( + bottom = insets.bottom + ) + WindowInsetsCompat.CONSUMED + } + v.requestApplyInsetsWhenAttached() + } +} + +fun View.requestApplyInsetsWhenAttached() { + if (isAttachedToWindow) { + // We're already attached, just request as normal + requestApplyInsets() + } else { + // We're not attached to the hierarchy, add a listener to + // request when we are + addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener { + override fun onViewAttachedToWindow(v: View) { + v.removeOnAttachStateChangeListener(this) + v.requestApplyInsets() + } + + override fun onViewDetachedFromWindow(v: View) = Unit + }) + } }
\ No newline at end of file diff --git a/main/src/ui/java/de/blinkt/openvpn/views/ScreenSlidePagerAdapter.kt b/main/src/ui/java/de/blinkt/openvpn/views/ScreenSlidePagerAdapter.kt index 0ce0afaa..98036953 100644 --- a/main/src/ui/java/de/blinkt/openvpn/views/ScreenSlidePagerAdapter.kt +++ b/main/src/ui/java/de/blinkt/openvpn/views/ScreenSlidePagerAdapter.kt @@ -7,11 +7,18 @@ package de.blinkt.openvpn.views import android.content.Context import android.content.res.Resources import android.os.Bundle +import android.view.View +import android.view.ViewGroup import androidx.annotation.StringRes +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat +import androidx.core.view.updateLayoutParams +import androidx.core.view.updatePadding import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.lifecycle.Lifecycle import androidx.viewpager2.adapter.FragmentStateAdapter +import de.blinkt.openvpn.R import java.util.Vector /** @@ -31,11 +38,13 @@ class ScreenSlidePagerAdapter(fm: FragmentManager, lc: Lifecycle, c: Context) : internal class Tab(var fragmentClass: Class<out Fragment>, var mName: String) private val mTabs = Vector<Tab>() + private var mBottomPadding= 0 override fun createFragment(position: Int): Fragment { try { val fragment = mTabs[position].fragmentClass.newInstance() if (mFragmentArguments != null) fragment.arguments = mFragmentArguments + return fragment } catch (e: InstantiationException) { e.printStackTrace() @@ -56,4 +65,8 @@ class ScreenSlidePagerAdapter(fm: FragmentManager, lc: Lifecycle, c: Context) : fun addTab(@StringRes name: Int, fragmentClass: Class<out Fragment>) { mTabs.add(Tab(fragmentClass, res.getString(name))) } + + fun setBottomPadding(bottom: Int) { + mBottomPadding = bottom; + } } |