Skip to content

Commit

Permalink
Hide Zeus mode for non-Zeus
Browse files Browse the repository at this point in the history
  • Loading branch information
niknetniko committed May 16, 2022
1 parent 010138c commit 2fe31db
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 36 deletions.
19 changes: 17 additions & 2 deletions app/src/main/java/be/ugent/zeus/hydra/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.shape.MaterialShapeDrawable;
import com.google.android.material.tabs.TabLayout;
import be.ugent.zeus.hydra.wpi.EnableManager;
import be.ugent.zeus.hydra.wpi.WpiActivity;
import dev.chrisbanes.insetter.Insetter;
import jonathanfinerty.once.Once;
Expand Down Expand Up @@ -188,7 +189,6 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> implements N
static final String ONCE_DRAWER = "once_drawer";
private static final String TAG = "BaseActivity";
private static final String UFORA = "com.d2l.brightspace.student.android";
private static final int ONBOARDING_REQUEST = 5;
private static final String STATE_IS_ONBOARDING_OPEN = "state_is_onboarding_open";
private static final String FRAGMENT_MENU_ID = "backStack";

Expand All @@ -197,6 +197,9 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> implements N
private static final String SHORTCUT_EVENTS = "events";
private static final String SHORTCUT_LIBRARIES = "libraries";

private static final int ONBOARDING_REQUEST = 5;
private static final int PREFERENCES_REQUEST = 5693;

private ActionBarDrawerToggle toggle;

private boolean isOnboardingOpen;
Expand Down Expand Up @@ -278,6 +281,8 @@ public void onDrawerClosed(View drawerView) {
}
}
});

updateMenuVisibility();

// If the instance is null, we must initialise a fragment, otherwise android does it for us.
if (savedInstanceState == null) {
Expand All @@ -303,6 +308,12 @@ public void onDrawerClosed(View drawerView) {
binding.drawerLayout.openDrawer(GravityCompat.START);
}
}

private void updateMenuVisibility() {
// Show Zeus-mode if enabled.
MenuItem item = binding.navigationView.getMenu().findItem(R.id.drawer_zeus);
item.setVisible(EnableManager.isZeusModeEnabled(this));
}

@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
Expand Down Expand Up @@ -336,7 +347,8 @@ private void selectDrawerItem(@NonNull MenuItem menuItem, @NavigationSource int
// First check if it are settings, then we don't update anything.
if (menuItem.getItemId() == R.id.drawer_pref) {
binding.drawerLayout.closeDrawer(GravityCompat.START);
PreferenceActivity.start(this, null);
Intent preferenceIntent = PreferenceActivity.startIntent(this, null);
startActivityForResult(preferenceIntent, PREFERENCES_REQUEST);
return;
}

Expand Down Expand Up @@ -559,6 +571,9 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.w(TAG, "Onboarding failed, stop app.");
finish();
}
} else if (requestCode == PREFERENCES_REQUEST) {
// Don't care about the actual status.
updateMenuVisibility();
}
// We need to call this for the fragments to work properly.
super.onActivityResult(requestCode, resultCode, data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,27 @@

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.preference.Preference;

import java.util.concurrent.atomic.AtomicInteger;

import be.ugent.zeus.hydra.BuildConfig;
import be.ugent.zeus.hydra.R;
import be.ugent.zeus.hydra.common.reporting.Reporting;
import be.ugent.zeus.hydra.common.ui.PreferenceFragment;
import be.ugent.zeus.hydra.common.ui.WebViewActivity;
import be.ugent.zeus.hydra.common.utils.NetworkUtils;
import be.ugent.zeus.hydra.wpi.EnableManager;

/**
* @author Niko Strijbol
*/
public class AboutFragment extends PreferenceFragment {

private static final int ZEUS_TIMES = 2;

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.pref_about, rootKey);
Expand All @@ -59,6 +66,29 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
return false;
});

// Ugly one-element array.
final AtomicInteger counter = new AtomicInteger();

// If Zeus-mode is enabled, link to the site.
// Otherwise, allow enabling it.
requirePreference("pref_about_creator_zeus").setOnPreferenceClickListener(preference -> {
if (EnableManager.isZeusModeEnabled(requireContext())) {
Toast.makeText(requireContext(), R.string.wpi_mode_enabled, Toast.LENGTH_SHORT).show();
NetworkUtils.maybeLaunchBrowser(requireContext(), "https://zeus.ugent.be");
} else {
int newValue = counter.incrementAndGet();
int remaining = ZEUS_TIMES - newValue;
if (remaining == 0) {
EnableManager.setZeusModeEnabled(requireContext(), true);
Toast.makeText(requireContext(), R.string.wpi_mode_enabled, Toast.LENGTH_SHORT).show();
} else {
String message = requireContext().getResources().getQuantityString(R.plurals.wpi_mode_press, remaining, remaining);
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show();
}
}
return true;
});

requirePreference("pref_about_creator_zeus")
.setIcon(AppCompatResources.getDrawable(requireContext(), R.drawable.logo_zeus));
requirePreference("pref_about_creator_dsa")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,19 @@ public class PreferenceActivity extends BaseActivity<ActivityPreferencesBinding>
private PreferenceEntry entry;

public static void start(@NonNull Context context, @Nullable PreferenceEntry entry) {
Intent intent = startIntent(context, entry);
context.startActivity(intent);
}

public static Intent startIntent(@NonNull Context context, @Nullable PreferenceEntry entry) {
Intent intent = new Intent(context, PreferenceActivity.class);
if (entry != null) {
intent.putExtra(ARG_FRAGMENT, (Parcelable) entry);
}
context.startActivity(intent);
return intent;
}


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down
72 changes: 72 additions & 0 deletions app/src/main/java/be/ugent/zeus/hydra/wpi/EnableManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2022 Niko Strijbol
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package be.ugent.zeus.hydra.wpi;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import androidx.core.content.pm.ShortcutInfoCompat;
import androidx.core.content.pm.ShortcutManagerCompat;
import androidx.core.graphics.drawable.IconCompat;
import androidx.preference.PreferenceManager;

import java.util.Collections;

import be.ugent.zeus.hydra.R;

/**
* Utilities to check if Zeus-mode has been enabled or not.
*
* @author Niko Strijbol
*/
public class EnableManager {
private static final String PREF_ENABLE_ZEUS_MODE = "pref_enable_zeus_mode";
private static final String ZEUS_SHORTCUT_ID = "be.ugent.zeus.hydra.shortcut.wpi";

private EnableManager() {
// No.
}

public static boolean isZeusModeEnabled(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getBoolean(PREF_ENABLE_ZEUS_MODE, false);
}

public static void setZeusModeEnabled(Context context, boolean enabled) {
if (enabled) {
ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(context, ZEUS_SHORTCUT_ID)
.setShortLabel("Zeus WPI")
.setLongLabel(context.getString(R.string.drawer_title_zeus))
.setIcon(IconCompat.createWithResource(context, R.drawable.logo_tap))
.setIntent(new Intent(Intent.ACTION_VIEW, null, context, WpiActivity.class))
.build();
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut);
} else {
ShortcutManagerCompat.removeDynamicShortcuts(context, Collections.singletonList(ZEUS_SHORTCUT_ID));
}
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
preferences.edit()
.putBoolean(PREF_ENABLE_ZEUS_MODE, enabled)
.apply();
}
}
1 change: 1 addition & 0 deletions app/src/main/res/menu/drawer_items.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
android:icon="@drawable/tabs_info"
android:title="@string/drawer_title_info" />
<item
android:visible="false"
android:id="@+id/drawer_zeus"
android:checkable="false"
android:icon="@drawable/logo_zeus"
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/res/values-en/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,11 @@
<string name="wpi_tap_form_error">There was an error while creating the order. Check that the order has not been saved before trying it again.</string>
<string name="wpi_tap_order_ok">Ordered for %1$s</string>
<string name="wpi_tap_form_confirm">Sure? The total of your order is %1$s.</string>

<string name="wpi_mode_enabled">Zeus mode is enabled.</string>
<plurals name="wpi_mode_press">
<item quantity="one">Press %d more time…</item>
<item quantity="other">Press %d more times…</item>
</plurals>

<!-- Accessibility strings -->
<string name="content_desc_more">more</string>
Expand Down
26 changes: 0 additions & 26 deletions app/src/main/res/values-night/colors.xml

This file was deleted.

4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@
<string name="wpi_tap_form_error">Er trad een fout op bij de bestelling. Controleer dat de bestelling er niet toch doorgekomen is voor u het opnieuw probeert.</string>
<string name="wpi_tap_order_ok">Besteld voor %1$s</string>
<string name="wpi_tap_form_confirm">Zeker? Het totaal van uw bestelling is %1$s.</string>
<string name="wpi_mode_enabled">Zeus-modus is ingeschakeld.</string>
<plurals name="wpi_mode_press">
<item quantity="other">Druk nog %d keer…</item>
</plurals>

<!-- Accessibility strings -->
<string name="content_desc_more">meer</string>
Expand Down
7 changes: 1 addition & 6 deletions app/src/main/res/xml/pref_about.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,7 @@
app:key="pref_about_creator_zeus"
app:persistent="false"
app:singleLineTitle="false"
app:title="@string/pref_about_creator_zeus"
tools:ignore="UnusedAttribute">
<intent
android:action="android.intent.action.VIEW"
android:data="https://zeus.ugent.be" />
</Preference>
app:title="@string/pref_about_creator_zeus" />

<Preference
app:iconSpaceReserved="true"
Expand Down

0 comments on commit 2fe31db

Please sign in to comment.