Skip to content

Commit

Permalink
Merge pull request #3 from HorizonDroid-13/13-staging
Browse files Browse the repository at this point in the history
13 staging
  • Loading branch information
xyzuniverse authored Oct 14, 2024
2 parents 0334be9 + 89fa456 commit b2b7cb7
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 26 deletions.
1 change: 1 addition & 0 deletions res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
<item name="android:textAppearance">@android:style/TextAppearance.DeviceDefault.Medium</item>
<item name="android:textColorHint">?android:attr/textColorSecondary</item>
<item name="android:minHeight">@dimen/min_tap_target_size</item>
<item name="android:maxLength">500</item>
</style>

<style name="wifi_section">
Expand Down
55 changes: 55 additions & 0 deletions src/com/android/settings/accounts/AccountTypePreferenceLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.collection.ArraySet;
import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceClickListener;
import androidx.preference.PreferenceFragmentCompat;
Expand All @@ -46,6 +50,8 @@
import com.android.settingslib.accounts.AuthenticatorHelper;
import com.android.settingslib.core.instrumentation.Instrumentable;

import java.util.Set;

/**
* Class to load the preference screen to be added to the settings page for the specific account
* type as specified in the account-authenticator.
Expand Down Expand Up @@ -83,6 +89,7 @@ public PreferenceScreen addPreferencesForType(final String accountType,
try {
desc = mAuthenticatorHelper.getAccountTypeDescription(accountType);
if (desc != null && desc.accountPreferencesId != 0) {
Set<String> fragmentAllowList = generateFragmentAllowlist(parent);
// Load the context of the target package, then apply the
// base Settings theme (no references to local resources)
// and create a context theme wrapper so that we get the
Expand All @@ -98,6 +105,12 @@ public PreferenceScreen addPreferencesForType(final String accountType,
themedCtx.getTheme().setTo(baseTheme);
prefs = mFragment.getPreferenceManager().inflateFromResource(themedCtx,
desc.accountPreferencesId, parent);
// Ignore Fragments provided dynamically, as these are coming from external
// applications which must not have access to internal Settings' fragments.
// These preferences are rendered into Settings, so they also won't have access
// to their own Fragments, meaning there is no acceptable usage of
// android:fragment here.
filterBlockedFragments(prefs, fragmentAllowList);
}
} catch (PackageManager.NameNotFoundException e) {
Log.w(TAG, "Couldn't load preferences.xml file from " + desc.packageName);
Expand Down Expand Up @@ -185,6 +198,48 @@ public boolean onPreferenceClick(Preference preference) {
}
}

// Build allowlist from existing Fragments in PreferenceGroup
@VisibleForTesting
Set<String> generateFragmentAllowlist(@Nullable PreferenceGroup prefs) {
Set<String> fragmentAllowList = new ArraySet<>();
if (prefs == null) {
return fragmentAllowList;
}

for (int i = 0; i < prefs.getPreferenceCount(); i++) {
Preference pref = prefs.getPreference(i);
if (pref instanceof PreferenceGroup) {
fragmentAllowList.addAll(generateFragmentAllowlist((PreferenceGroup) pref));
}

String fragmentName = pref.getFragment();
if (!TextUtils.isEmpty(fragmentName)) {
fragmentAllowList.add(fragmentName);
}
}
return fragmentAllowList;
}

// Block clicks on any Preference with android:fragment that is not contained in the allowlist
@VisibleForTesting
void filterBlockedFragments(@Nullable PreferenceGroup prefs,
@NonNull Set<String> allowedFragments) {
if (prefs == null) {
return;
}
for (int i = 0; i < prefs.getPreferenceCount(); i++) {
Preference pref = prefs.getPreference(i);
if (pref instanceof PreferenceGroup) {
filterBlockedFragments((PreferenceGroup) pref, allowedFragments);
}

String fragmentName = pref.getFragment();
if (fragmentName != null && !allowedFragments.contains(fragmentName)) {
pref.setOnPreferenceClickListener(preference -> true);
}
}
}

/**
* Determines if the supplied Intent is safe. A safe intent is one that is
* will launch a exported=true activity or owned by the same uid as the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ public void onResume() {
}
}

@Override
protected boolean shouldSkipForInitialSUW() {
return true;
}

@Override
public void onPause() {
super.onPause();
Expand Down
10 changes: 10 additions & 0 deletions src/com/android/settings/homepage/SettingsHomepageActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ public CategoryMixin getCategoryMixin() {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Ensure device is provisioned in order to access Settings home
// TODO(b/331254029): This should later be replaced in favor of an allowlist
boolean unprovisioned = android.provider.Settings.Global.getInt(getContentResolver(),
android.provider.Settings.Global.DEVICE_PROVISIONED, 0) == 0;
if (unprovisioned) {
Log.e(TAG, "Device is not provisioned, exiting Settings");
finish();
return;
}

mIsEmbeddingActivityEnabled = ActivityEmbeddingUtils.isEmbeddingActivityEnabled(this);
if (mIsEmbeddingActivityEnabled) {
final UserManager um = getSystemService(UserManager.class);
Expand Down
2 changes: 1 addition & 1 deletion src/com/android/settings/search/SearchFeatureProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public interface SearchFeatureProvider {
* @throws IllegalArgumentException when caller is null
* @throws SecurityException when caller is not allowed to launch search result page
*/
void verifyLaunchSearchResultPageCaller(Context context, @NonNull ComponentName caller)
void verifyLaunchSearchResultPageCaller(@NonNull Context context, @NonNull String callerPackage)
throws SecurityException, IllegalArgumentException;

/**
Expand Down
18 changes: 8 additions & 10 deletions src/com/android/settings/search/SearchFeatureProviderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@

package com.android.settings.search;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.android.settingslib.search.SearchIndexableResources;
import com.android.settingslib.search.SearchIndexableResourcesMobile;

Expand All @@ -32,21 +33,18 @@
*/
public class SearchFeatureProviderImpl implements SearchFeatureProvider {

private static final String TAG = "SearchFeatureProvider";

private SearchIndexableResources mSearchIndexableResources;

@Override
public void verifyLaunchSearchResultPageCaller(Context context, ComponentName caller) {
if (caller == null) {
public void verifyLaunchSearchResultPageCaller(@NonNull Context context,
@NonNull String callerPackage) {
if (TextUtils.isEmpty(callerPackage)) {
throw new IllegalArgumentException("ExternalSettingsTrampoline intents "
+ "must be called with startActivityForResult");
}
final String packageName = caller.getPackageName();
final boolean isSettingsPackage = TextUtils.equals(packageName, context.getPackageName())
|| TextUtils.equals(getSettingsIntelligencePkgName(context), packageName);
final boolean isAllowlistedPackage =
isSignatureAllowlisted(context, caller.getPackageName());
final boolean isSettingsPackage = TextUtils.equals(callerPackage, context.getPackageName())
|| TextUtils.equals(getSettingsIntelligencePkgName(context), callerPackage);
final boolean isAllowlistedPackage = isSignatureAllowlisted(context, callerPackage);
if (isSettingsPackage || isAllowlistedPackage) {
return;
}
Expand Down
13 changes: 6 additions & 7 deletions src/com/android/settings/search/SearchResultTrampoline.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static com.android.settings.SettingsActivity.EXTRA_SHOW_FRAGMENT_TAB;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
Expand Down Expand Up @@ -51,11 +50,11 @@ public class SearchResultTrampoline extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

final ComponentName callingActivity = getCallingActivity();
final String callerPackage = getLaunchedFromPackage();
// First make sure caller has privilege to launch a search result page.
FeatureFactory.getFactory(this)
.getSearchFeatureProvider()
.verifyLaunchSearchResultPageCaller(this, callingActivity);
.verifyLaunchSearchResultPageCaller(this, callerPackage);
// Didn't crash, proceed and launch the result as a subsetting.
Intent intent = getIntent();
final String highlightMenuKey = intent.getStringExtra(
Expand Down Expand Up @@ -99,7 +98,7 @@ protected void onCreate(Bundle savedInstanceState) {

if (!ActivityEmbeddingUtils.isEmbeddingActivityEnabled(this)) {
startActivity(intent);
} else if (isSettingsIntelligence(callingActivity)) {
} else if (isSettingsIntelligence(callerPackage)) {
if (FeatureFlagUtils.isEnabled(this, FeatureFlags.SETTINGS_SEARCH_ALWAYS_EXPAND)) {
startActivity(SettingsActivity.getTrampolineIntent(intent, highlightMenuKey)
.setClass(this, DeepLinkHomepageActivityInternal.class)
Expand Down Expand Up @@ -132,9 +131,9 @@ protected void onCreate(Bundle savedInstanceState) {
finish();
}

private boolean isSettingsIntelligence(ComponentName callingActivity) {
return callingActivity != null && TextUtils.equals(
callingActivity.getPackageName(),
private boolean isSettingsIntelligence(String callerPackage) {
return TextUtils.equals(
callerPackage,
FeatureFactory.getFactory(this).getSearchFeatureProvider()
.getSettingsIntelligencePkgName(this));
}
Expand Down
20 changes: 20 additions & 0 deletions src/com/android/settings/wifi/AddNetworkFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@

package com.android.settings.wifi;

import static android.os.UserManager.DISALLOW_ADD_WIFI_CONFIG;

import android.app.Activity;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiConfiguration;
import android.os.Bundle;
import android.os.UserManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -41,6 +46,7 @@
*/
public class AddNetworkFragment extends InstrumentedFragment implements WifiConfigUiBase2,
View.OnClickListener {
private static final String TAG = "AddNetworkFragment";

public static final String WIFI_CONFIG_KEY = "wifi_config_key";
@VisibleForTesting
Expand All @@ -58,6 +64,10 @@ public class AddNetworkFragment extends InstrumentedFragment implements WifiConf
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isAddWifiConfigAllowed(getContext())) {
getActivity().finish();
return;
}
}

@Override
Expand Down Expand Up @@ -209,4 +219,14 @@ void handleCancelAction() {
activity.setResult(Activity.RESULT_CANCELED);
activity.finish();
}

@VisibleForTesting
static boolean isAddWifiConfigAllowed(Context context) {
UserManager userManager = context.getSystemService(UserManager.class);
if (userManager != null && userManager.hasUserRestriction(DISALLOW_ADD_WIFI_CONFIG)) {
Log.e(TAG, "The user is not allowed to add Wi-Fi configuration.");
return false;
}
return true;
}
}
13 changes: 12 additions & 1 deletion src/com/android/settings/wifi/WifiDialogActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.android.settings.wifi;

import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.os.UserManager.DISALLOW_ADD_WIFI_CONFIG;
import static android.os.UserManager.DISALLOW_CONFIG_WIFI;

import android.app.KeyguardManager;
Expand Down Expand Up @@ -122,7 +123,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

super.onCreate(savedInstanceState);
if (!isConfigWifiAllowed()) {
if (!isConfigWifiAllowed() || !isAddWifiConfigAllowed()) {
finish();
return;
}
Expand Down Expand Up @@ -393,6 +394,16 @@ boolean isConfigWifiAllowed() {
return isConfigWifiAllowed;
}

@VisibleForTesting
boolean isAddWifiConfigAllowed() {
UserManager userManager = getSystemService(UserManager.class);
if (userManager != null && userManager.hasUserRestriction(DISALLOW_ADD_WIFI_CONFIG)) {
Log.e(TAG, "The user is not allowed to add Wi-Fi configuration.");
return false;
}
return true;
}

private boolean hasWifiManager() {
if (mWifiManager != null) return true;
mWifiManager = getSystemService(WifiManager.class);
Expand Down
21 changes: 21 additions & 0 deletions src/com/android/settings/wifi/dpp/WifiDppConfiguratorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.android.settings.wifi.dpp;

import static android.os.UserManager.DISALLOW_ADD_WIFI_CONFIG;

import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -99,6 +101,10 @@ public int getMetricsCategory() {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isAddWifiConfigAllowed(getApplicationContext())) {
finish();
return;
}

if (savedInstanceState != null) {
String qrCode = savedInstanceState.getString(KEY_QR_CODE);
Expand All @@ -119,6 +125,11 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
protected void handleIntent(Intent intent) {
if (!isAddWifiConfigAllowed(getApplicationContext())) {
finish();
return;
}

if (isGuestUser(getApplicationContext())) {
Log.e(TAG, "Guest user is not allowed to configure Wi-Fi!");
EventLog.writeEvent(0x534e4554, "224772890", -1 /* UID */, "User is a guest");
Expand Down Expand Up @@ -402,4 +413,14 @@ private static boolean isGuestUser(Context context) {
if (userManager == null) return false;
return userManager.isGuestUser();
}

@VisibleForTesting
static boolean isAddWifiConfigAllowed(Context context) {
UserManager userManager = context.getSystemService(UserManager.class);
if (userManager != null && userManager.hasUserRestriction(DISALLOW_ADD_WIFI_CONFIG)) {
Log.e(TAG, "The user is not allowed to add Wi-Fi configuration.");
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -895,4 +895,9 @@ public void notifyBackupManager_triStateIsNotEnabled_notInvokeDataChanged() {

verifyNoInteractions(mBackupManager);
}

@Test
public void shouldSkipForInitialSUW_returnTrue() {
assertThat(mFragment.shouldSkipForInitialSUW()).isTrue();
}
}
Loading

0 comments on commit b2b7cb7

Please sign in to comment.