Re-add the progress bar for storage preferences.

This re-adds the progress bar and unifies the
StorageItemPreference and its alternate.

Change-Id: Iefc12b4b554306c706c83be232175dd2b84227b7
Fixes: 36223433
Test: Settings robotest
This commit is contained in:
Daniel Nishi
2017-03-15 14:00:13 -07:00
parent 9392681afa
commit cf0e659a6c
14 changed files with 183 additions and 203 deletions

View File

@@ -31,25 +31,25 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"> android:orientation="horizontal">
<TextView <TextView
android:id="@android:id/title" android:id="@android:id/title"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:singleLine="true" android:singleLine="true"
android:textAppearance="@android:style/TextAppearance.Material.Subhead" android:textAppearance="@android:style/TextAppearance.Material.Subhead"
android:textAlignment="viewStart" android:textAlignment="viewStart"
android:ellipsize="marquee" android:ellipsize="marquee"
android:fadingEdge="horizontal" /> android:fadingEdge="horizontal" />
<TextView <TextView
android:id="@android:id/summary" android:id="@android:id/summary"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textAlignment="viewStart" android:textAlignment="viewStart"
android:textAppearance="@android:style/TextAppearance.Material.Body1" android:textAppearance="@android:style/TextAppearance.Material.Body1"
android:textColor="?android:attr/textColorSecondaryNoDisable" android:textColor="?android:attr/textColorSecondaryNoDisable"
android:maxLines="10" /> android:maxLines="10" />
</LinearLayout> </LinearLayout>

View File

@@ -1,56 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:gravity="center_vertical"
android:paddingStart="@dimen/preference_no_icon_padding_start"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingTop="16dip"
android:paddingBottom="16dip"
android:background="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@android:id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:textAppearance="@android:style/TextAppearance.Material.Subhead"
android:textAlignment="viewStart"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
<TextView
android:id="@android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="viewStart"
android:textAppearance="@android:style/TextAppearance.Material.Body1"
android:textColor="?android:attr/textColorSecondaryNoDisable"
android:maxLines="10" />
</LinearLayout>
</LinearLayout>

View File

@@ -30,28 +30,28 @@
android:order="1" android:order="1"
settings:allowDividerAbove="true" settings:allowDividerAbove="true"
settings:allowDividerBelow="true"/> settings:allowDividerBelow="true"/>
<com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate <com.android.settings.deviceinfo.StorageItemPreference
android:key="pref_photos_videos" android:key="pref_photos_videos"
android:title="@string/storage_photos_videos" android:title="@string/storage_photos_videos"
android:order="2" settings:allowDividerAbove="true"
settings:allowDividerAbove="true" /> android:order="2" />
<com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate <com.android.settings.deviceinfo.StorageItemPreference
android:key="pref_music_audio" android:key="pref_music_audio"
android:title="@string/storage_music_audio" android:title="@string/storage_music_audio"
android:order="3" /> android:order="3" />
<com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate <com.android.settings.deviceinfo.StorageItemPreference
android:key="pref_games" android:key="pref_games"
android:title="@string/storage_games" android:title="@string/storage_games"
android:order="4" /> android:order="4" />
<com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate <com.android.settings.deviceinfo.StorageItemPreference
android:key="pref_other_apps" android:key="pref_other_apps"
android:title="@string/storage_other_apps" android:title="@string/storage_other_apps"
android:order="5" /> android:order="5" />
<com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate <com.android.settings.deviceinfo.StorageItemPreference
android:key="pref_files" android:key="pref_files"
android:title="@string/storage_files" android:title="@string/storage_files"
android:order="6" /> android:order="6" />
<com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate <com.android.settings.deviceinfo.StorageItemPreference
android:key="pref_system" android:key="pref_system"
android:title="@string/storage_detail_system" android:title="@string/storage_detail_system"
android:order="100" /> android:order="100" />

View File

@@ -18,24 +18,24 @@
<PreferenceScreen <PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/storage_settings"> android:title="@string/storage_settings">
<com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate <com.android.settings.deviceinfo.StorageItemPreference
android:key="pref_photos_videos" android:key="pref_photos_videos"
android:title="@string/storage_photos_videos"> android:title="@string/storage_photos_videos">
</com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate> </com.android.settings.deviceinfo.StorageItemPreference>
<com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate <com.android.settings.deviceinfo.StorageItemPreference
android:key="pref_music_audio" android:key="pref_music_audio"
android:title="@string/storage_music_audio"> android:title="@string/storage_music_audio">
</com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate> </com.android.settings.deviceinfo.StorageItemPreference>
<com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate <com.android.settings.deviceinfo.StorageItemPreference
android:key="pref_games" android:key="pref_games"
android:title="@string/storage_games"> android:title="@string/storage_games">
</com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate> </com.android.settings.deviceinfo.StorageItemPreference>
<com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate <com.android.settings.deviceinfo.StorageItemPreference
android:key="pref_other_apps" android:key="pref_other_apps"
android:title="@string/storage_other_apps"> android:title="@string/storage_other_apps">
</com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate> </com.android.settings.deviceinfo.StorageItemPreference>
<com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate <com.android.settings.deviceinfo.StorageItemPreference
android:key="pref_files" android:key="pref_files"
android:title="@string/storage_files"> android:title="@string/storage_files">
</com.android.settings.deviceinfo.storage.StorageItemPreferenceAlternate> </com.android.settings.deviceinfo.StorageItemPreference>
</PreferenceScreen> </PreferenceScreen>

View File

@@ -114,6 +114,16 @@ public class StorageDashboardFragment extends DashboardFragment
mSummaryController.updateBytes(usedBytes, totalSize); mSummaryController.updateBytes(usedBytes, totalSize);
mPreferenceController.setVolume(mVolume); mPreferenceController.setVolume(mVolume);
mPreferenceController.setSystemSize(systemSize); mPreferenceController.setSystemSize(systemSize);
mPreferenceController.setTotalSize(totalSize);
for (int i = 0, size = mSecondaryUsers.size(); i < size; i++) {
PreferenceController controller = mSecondaryUsers.get(i);
if (controller instanceof SecondaryUserController) {
SecondaryUserController userController = (SecondaryUserController) controller;
userController.setTotalSize(totalSize);
}
}
} }
@Override @Override

View File

@@ -20,6 +20,7 @@ import android.content.Context;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceViewHolder; import android.support.v7.preference.PreferenceViewHolder;
import android.text.format.Formatter; import android.text.format.Formatter;
import android.util.AttributeSet;
import android.view.View; import android.view.View;
import android.widget.ProgressBar; import android.widget.ProgressBar;
@@ -28,13 +29,18 @@ import com.android.settings.R;
public class StorageItemPreference extends Preference { public class StorageItemPreference extends Preference {
public int userHandle; public int userHandle;
private ProgressBar progressBar; private ProgressBar mProgressBar;
private static final int PROGRESS_MAX = 100; private static final int PROGRESS_MAX = 100;
private int progress = -1; private int mProgressPercent = -1;
public StorageItemPreference(Context context) { public StorageItemPreference(Context context) {
super(context); this(context, null);
}
public StorageItemPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setLayoutResource(R.layout.storage_item); setLayoutResource(R.layout.storage_item);
setSummary(R.string.memory_calculating_size);
} }
public void setStorageSize(long size, long total) { public void setStorageSize(long size, long total) {
@@ -42,30 +48,30 @@ public class StorageItemPreference extends Preference {
? String.valueOf(0) ? String.valueOf(0)
: Formatter.formatFileSize(getContext(), size)); : Formatter.formatFileSize(getContext(), size));
if (total == 0) { if (total == 0) {
progress = 0; mProgressPercent = 0;
} else { } else {
progress = (int)(size * PROGRESS_MAX / total); mProgressPercent = (int)(size * PROGRESS_MAX / total);
} }
updateProgressBar(); updateProgressBar();
} }
protected void updateProgressBar() { protected void updateProgressBar() {
if (progressBar == null) if (mProgressBar == null)
return; return;
if (progress == -1) { if (mProgressPercent == -1) {
progressBar.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE);
return; return;
} }
progressBar.setVisibility(View.VISIBLE); mProgressBar.setVisibility(View.VISIBLE);
progressBar.setMax(PROGRESS_MAX); mProgressBar.setMax(PROGRESS_MAX);
progressBar.setProgress(progress); mProgressBar.setProgress(mProgressPercent);
} }
@Override @Override
public void onBindViewHolder(PreferenceViewHolder view) { public void onBindViewHolder(PreferenceViewHolder view) {
progressBar = (ProgressBar) view.findViewById(android.R.id.progress); mProgressBar = (ProgressBar) view.findViewById(android.R.id.progress);
updateProgressBar(); updateProgressBar();
super.onBindViewHolder(view); super.onBindViewHolder(view);
} }

View File

@@ -28,6 +28,7 @@ import android.util.SparseArray;
import com.android.settings.Utils; import com.android.settings.Utils;
import com.android.settings.applications.UserManagerWrapper; import com.android.settings.applications.UserManagerWrapper;
import com.android.settings.core.PreferenceController; import com.android.settings.core.PreferenceController;
import com.android.settings.deviceinfo.StorageItemPreference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -46,8 +47,9 @@ public class SecondaryUserController extends PreferenceController implements
private static final int SIZE_NOT_SET = -1; private static final int SIZE_NOT_SET = -1;
private @NonNull UserInfo mUser; private @NonNull UserInfo mUser;
private @Nullable StorageItemPreferenceAlternate mStoragePreference; private @Nullable StorageItemPreference mStoragePreference;
private long mSize; private long mSize;
private long mTotalSizeBytes;
/** /**
* Adds the appropriate controllers to a controller list for handling all secondary users on * Adds the appropriate controllers to a controller list for handling all secondary users on
@@ -98,14 +100,14 @@ public class SecondaryUserController extends PreferenceController implements
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
if (mStoragePreference == null) { if (mStoragePreference == null) {
mStoragePreference = new StorageItemPreferenceAlternate(screen.getContext()); mStoragePreference = new StorageItemPreference(screen.getContext());
PreferenceGroup group = PreferenceGroup group =
(PreferenceGroup) screen.findPreference(TARGET_PREFERENCE_GROUP_KEY); (PreferenceGroup) screen.findPreference(TARGET_PREFERENCE_GROUP_KEY);
mStoragePreference.setTitle(mUser.name); mStoragePreference.setTitle(mUser.name);
mStoragePreference.setKey(PREFERENCE_KEY_BASE + mUser.id); mStoragePreference.setKey(PREFERENCE_KEY_BASE + mUser.id);
if (mSize != SIZE_NOT_SET) { if (mSize != SIZE_NOT_SET) {
mStoragePreference.setStorageSize(mSize); mStoragePreference.setStorageSize(mSize, mTotalSizeBytes);
} }
group.setVisible(true); group.setVisible(true);
group.addPreference(mStoragePreference); group.addPreference(mStoragePreference);
@@ -137,10 +139,18 @@ public class SecondaryUserController extends PreferenceController implements
public void setSize(long size) { public void setSize(long size) {
mSize = size; mSize = size;
if (mStoragePreference != null) { if (mStoragePreference != null) {
mStoragePreference.setStorageSize(mSize); mStoragePreference.setStorageSize(mSize, mTotalSizeBytes);
} }
} }
/**
* Sets the total size for the preference for the progress bar.
* @param totalSizeBytes Total size in bytes.
*/
public void setTotalSize(long totalSizeBytes) {
mTotalSizeBytes = totalSizeBytes;
}
public void handleResult(SparseArray<StorageAsyncLoader.AppsStorageResult> stats) { public void handleResult(SparseArray<StorageAsyncLoader.AppsStorageResult> stats) {
int userId = getUser().id; int userId = getUser().id;
StorageAsyncLoader.AppsStorageResult result = stats.get(userId); StorageAsyncLoader.AppsStorageResult result = stats.get(userId);

View File

@@ -1,42 +0,0 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.deviceinfo.storage;
import android.content.Context;
import android.support.v7.preference.Preference;
import android.text.format.Formatter;
import android.util.AttributeSet;
import com.android.settings.R;
public class StorageItemPreferenceAlternate extends Preference {
public StorageItemPreferenceAlternate(Context context) {
this(context, null);
}
public StorageItemPreferenceAlternate(Context context, AttributeSet attrs) {
super(context, attrs);
setLayoutResource(R.layout.storage_item_alternate);
setSummary(R.string.memory_calculating_size);
}
public void setStorageSize(long size) {
setSummary(size == 0
? String.valueOf(0)
: Formatter.formatFileSize(getContext(), size));
}
}

View File

@@ -35,6 +35,7 @@ import com.android.settings.Utils;
import com.android.settings.applications.ManageApplications; import com.android.settings.applications.ManageApplications;
import com.android.settings.core.PreferenceController; import com.android.settings.core.PreferenceController;
import com.android.settings.core.instrumentation.MetricsFeatureProvider; import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.deviceinfo.StorageItemPreference;
import com.android.settings.overlay.FeatureFactory; import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.deviceinfo.StorageMeasurement; import com.android.settingslib.deviceinfo.StorageMeasurement;
import com.android.settingslib.deviceinfo.StorageVolumeProvider; import com.android.settingslib.deviceinfo.StorageVolumeProvider;
@@ -71,13 +72,14 @@ public class StorageItemPreferenceController extends PreferenceController {
private VolumeInfo mVolume; private VolumeInfo mVolume;
private int mUserId; private int mUserId;
private long mSystemSize; private long mSystemSize;
private long mTotalSize;
private StorageItemPreferenceAlternate mPhotoPreference; private StorageItemPreference mPhotoPreference;
private StorageItemPreferenceAlternate mAudioPreference; private StorageItemPreference mAudioPreference;
private StorageItemPreferenceAlternate mGamePreference; private StorageItemPreference mGamePreference;
private StorageItemPreferenceAlternate mAppPreference; private StorageItemPreference mAppPreference;
private StorageItemPreferenceAlternate mFilePreference; private StorageItemPreference mFilePreference;
private StorageItemPreferenceAlternate mSystemPreference; private StorageItemPreference mSystemPreference;
private static final String AUTHORITY_MEDIA = "com.android.providers.media.documents"; private static final String AUTHORITY_MEDIA = "com.android.providers.media.documents";
@@ -164,37 +166,38 @@ public class StorageItemPreferenceController extends PreferenceController {
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
mPhotoPreference = (StorageItemPreferenceAlternate) screen.findPreference(PHOTO_KEY); mPhotoPreference = (StorageItemPreference) screen.findPreference(PHOTO_KEY);
mAudioPreference = (StorageItemPreferenceAlternate) screen.findPreference(AUDIO_KEY); mAudioPreference = (StorageItemPreference) screen.findPreference(AUDIO_KEY);
mGamePreference = (StorageItemPreferenceAlternate) screen.findPreference(GAME_KEY); mGamePreference = (StorageItemPreference) screen.findPreference(GAME_KEY);
mAppPreference = (StorageItemPreferenceAlternate) screen.findPreference(OTHER_APPS_KEY); mAppPreference = (StorageItemPreference) screen.findPreference(OTHER_APPS_KEY);
mSystemPreference = (StorageItemPreferenceAlternate) screen.findPreference(SYSTEM_KEY); mSystemPreference = (StorageItemPreference) screen.findPreference(SYSTEM_KEY);
mFilePreference = (StorageItemPreferenceAlternate) screen.findPreference(FILES_KEY); mFilePreference = (StorageItemPreference) screen.findPreference(FILES_KEY);
} }
public void onLoadFinished(StorageAsyncLoader.AppsStorageResult data) { public void onLoadFinished(StorageAsyncLoader.AppsStorageResult data) {
// TODO(b/35927909): Figure out how to split out apps which are only installed for work // TODO(b/35927909): Figure out how to split out apps which are only installed for work
// profiles in order to attribute those app's code bytes only to that profile. // profiles in order to attribute those app's code bytes only to that profile.
mPhotoPreference.setStorageSize( mPhotoPreference.setStorageSize(
data.externalStats.imageBytes + data.externalStats.videoBytes); data.externalStats.imageBytes + data.externalStats.videoBytes, mTotalSize);
mAudioPreference.setStorageSize(data.musicAppsSize + data.externalStats.audioBytes); mAudioPreference.setStorageSize(
mGamePreference.setStorageSize(data.gamesSize); data.musicAppsSize + data.externalStats.audioBytes, mTotalSize);
mAppPreference.setStorageSize(data.otherAppsSize); mGamePreference.setStorageSize(data.gamesSize, mTotalSize);
mAppPreference.setStorageSize(data.otherAppsSize, mTotalSize);
if (mSystemPreference != null) { if (mSystemPreference != null) {
mSystemPreference.setStorageSize(mSystemSize + data.systemSize); mSystemPreference.setStorageSize(mSystemSize + data.systemSize, mTotalSize);
} }
long unattributedBytes = data.externalStats.totalBytes - data.externalStats.audioBytes long unattributedBytes = data.externalStats.totalBytes - data.externalStats.audioBytes
- data.externalStats.videoBytes - data.externalStats.imageBytes; - data.externalStats.videoBytes - data.externalStats.imageBytes;
mFilePreference.setStorageSize(unattributedBytes); mFilePreference.setStorageSize(unattributedBytes, mTotalSize);
} }
/** public void setSystemSize(long systemSizeBytes) {
* Sets the system size for the system size preference. mSystemSize = systemSizeBytes;
* @param systemSize the size of the system in bytes }
*/
public void setSystemSize(long systemSize) { public void setTotalSize(long totalSizeBytes) {
mSystemSize = systemSize; mTotalSize = totalSizeBytes;
} }
/** /**

View File

@@ -29,6 +29,7 @@ import com.android.internal.logging.nano.MetricsProto;
import com.android.internal.util.Preconditions; import com.android.internal.util.Preconditions;
import com.android.settings.Utils; import com.android.settings.Utils;
import com.android.settings.core.PreferenceController; import com.android.settings.core.PreferenceController;
import com.android.settings.deviceinfo.StorageItemPreference;
import com.android.settings.deviceinfo.StorageProfileFragment; import com.android.settings.deviceinfo.StorageProfileFragment;
import com.android.settingslib.drawer.SettingsDrawerActivity; import com.android.settingslib.drawer.SettingsDrawerActivity;
@@ -38,8 +39,9 @@ import com.android.settingslib.drawer.SettingsDrawerActivity;
public class UserProfileController extends PreferenceController implements public class UserProfileController extends PreferenceController implements
StorageAsyncLoader.ResultHandler { StorageAsyncLoader.ResultHandler {
private static final String PREFERENCE_KEY_BASE = "pref_profile_"; private static final String PREFERENCE_KEY_BASE = "pref_profile_";
private StorageItemPreferenceAlternate mStoragePreference; private StorageItemPreference mStoragePreference;
private UserInfo mUser; private UserInfo mUser;
private long mTotalSizeBytes;
private final int mPreferenceOrder; private final int mPreferenceOrder;
public UserProfileController(Context context, UserInfo info, int preferenceOrder) { public UserProfileController(Context context, UserInfo info, int preferenceOrder) {
@@ -60,7 +62,7 @@ public class UserProfileController extends PreferenceController implements
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
mStoragePreference = new StorageItemPreferenceAlternate(screen.getContext()); mStoragePreference = new StorageItemPreference(screen.getContext());
mStoragePreference.setOrder(mPreferenceOrder); mStoragePreference.setOrder(mPreferenceOrder);
mStoragePreference.setKey(PREFERENCE_KEY_BASE + mUser.id); mStoragePreference.setKey(PREFERENCE_KEY_BASE + mUser.id);
mStoragePreference.setTitle(mUser.name); mStoragePreference.setTitle(mUser.name);
@@ -91,16 +93,20 @@ public class UserProfileController extends PreferenceController implements
int userId = mUser.id; int userId = mUser.id;
StorageAsyncLoader.AppsStorageResult result = stats.get(userId); StorageAsyncLoader.AppsStorageResult result = stats.get(userId);
if (result != null) { if (result != null) {
setSize(result.externalStats.totalBytes); setSize(result.externalStats.totalBytes, mTotalSizeBytes);
} }
} }
/** /**
* Sets the size for the preference using a byte count. * Sets the size for the preference using a byte count.
*/ */
public void setSize(long size) { public void setSize(long size, long totalSize) {
if (mStoragePreference != null) { if (mStoragePreference != null) {
mStoragePreference.setStorageSize(size); mStoragePreference.setStorageSize(size, totalSize);
} }
} }
public void setTotalSize(long totalSize) {
mTotalSizeBytes = totalSize;
}
} }

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.settings;
/**
* Convenience methods and constants for testing.
*/
public class TestUtils {
public static final long KILOBYTE = 1024L; // TODO: Change to 1000 in O Robolectric.
public static final long MEGABYTE = KILOBYTE * KILOBYTE;
public static final long GIGABYTE = KILOBYTE * MEGABYTE;
}

View File

@@ -13,11 +13,16 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License * limitations under the License
*/ */
package com.android.settings.deviceinfo.storage; package com.android.settings.deviceinfo;
import static com.android.settings.TestUtils.KILOBYTE;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import android.content.Context; import android.content.Context;
import android.support.v7.preference.PreferenceViewHolder;
import android.view.LayoutInflater;
import android.widget.ProgressBar;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.SettingsRobolectricTestRunner;
@@ -29,28 +34,41 @@ import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class StorageItemPreferenceAlternateTest { public class StorageItemPreferenceTest {
private Context mContext; private Context mContext;
private StorageItemPreference mPreference;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
mContext = RuntimeEnvironment.application; mContext = RuntimeEnvironment.application;
mPreference = new StorageItemPreference(mContext);
} }
@Test @Test
public void testBeforeLoad() { public void testBeforeLoad() {
StorageItemPreferenceAlternate pref = new StorageItemPreferenceAlternate(mContext); assertThat(mPreference.getSummary()).isEqualTo(
assertThat(((String) pref.getSummary())).isEqualTo(
mContext.getString(R.string.memory_calculating_size)); mContext.getString(R.string.memory_calculating_size));
} }
@Test @Test
public void testAfterLoad() { public void testAfterLoad() {
StorageItemPreferenceAlternate pref = new StorageItemPreferenceAlternate(mContext); mPreference.setStorageSize(KILOBYTE, KILOBYTE * 10);
pref.setStorageSize(1024L); assertThat(((String) mPreference.getSummary())).isEqualTo("1.00KB");
assertThat(((String) pref.getSummary())).isEqualTo("1.00KB");
} }
@Test
public void testProgressBarPercentageSet() {
final PreferenceViewHolder holder = new PreferenceViewHolder(
LayoutInflater.from(mContext).inflate(R.layout.storage_item, null));
final ProgressBar progressBar =
(ProgressBar) holder.itemView.findViewById(android.R.id.progress);
mPreference.onBindViewHolder(holder);
mPreference.setStorageSize(KILOBYTE, KILOBYTE * 10);
assertThat(progressBar.getProgress()).isEqualTo(10);
}
} }

View File

@@ -15,6 +15,8 @@
*/ */
package com.android.settings.deviceinfo.storage; package com.android.settings.deviceinfo.storage;
import static com.android.settings.TestUtils.KILOBYTE;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
@@ -43,6 +45,7 @@ import com.android.settings.SubSettings;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
import com.android.settings.applications.ManageApplications; import com.android.settings.applications.ManageApplications;
import com.android.settings.core.instrumentation.MetricsFeatureProvider; import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.deviceinfo.StorageItemPreference;
import com.android.settings.testutils.FakeFeatureFactory; import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.applications.StorageStatsSource; import com.android.settingslib.applications.StorageStatsSource;
import com.android.settingslib.deviceinfo.StorageVolumeProvider; import com.android.settingslib.deviceinfo.StorageVolumeProvider;
@@ -60,11 +63,6 @@ import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class StorageItemPreferenceControllerTest { public class StorageItemPreferenceControllerTest {
/**
* In O, this will change to 1000 instead of 1024 due to the formatter properly defining a
* kilobyte.
*/
private static long KILOBYTE = 1024L;
private Context mContext; private Context mContext;
private VolumeInfo mVolume; private VolumeInfo mVolume;
@Mock(answer = Answers.RETURNS_DEEP_STUBS) @Mock(answer = Answers.RETURNS_DEEP_STUBS)
@@ -72,7 +70,7 @@ public class StorageItemPreferenceControllerTest {
@Mock @Mock
private StorageVolumeProvider mSvp; private StorageVolumeProvider mSvp;
private StorageItemPreferenceController mController; private StorageItemPreferenceController mController;
private StorageItemPreferenceAlternate mPreference; private StorageItemPreference mPreference;
private FakeFeatureFactory mFakeFeatureFactory; private FakeFeatureFactory mFakeFeatureFactory;
private MetricsFeatureProvider mMetricsFeatureProvider; private MetricsFeatureProvider mMetricsFeatureProvider;
@@ -87,7 +85,7 @@ public class StorageItemPreferenceControllerTest {
// Note: null is passed as the Lifecycle because we are handling it outside of the normal // Note: null is passed as the Lifecycle because we are handling it outside of the normal
// Settings fragment lifecycle for test purposes. // Settings fragment lifecycle for test purposes.
mController = new StorageItemPreferenceController(mContext, mFragment, mVolume, mSvp); mController = new StorageItemPreferenceController(mContext, mFragment, mVolume, mSvp);
mPreference = new StorageItemPreferenceAlternate(mContext); mPreference = new StorageItemPreference(mContext);
// Inflate the preference and the widget. // Inflate the preference and the widget.
LayoutInflater inflater = LayoutInflater.from(mContext); LayoutInflater inflater = LayoutInflater.from(mContext);
@@ -190,12 +188,12 @@ public class StorageItemPreferenceControllerTest {
@Test @Test
public void testMeasurementCompletedUpdatesPreferences() { public void testMeasurementCompletedUpdatesPreferences() {
StorageItemPreferenceAlternate audio = new StorageItemPreferenceAlternate(mContext); StorageItemPreference audio = new StorageItemPreference(mContext);
StorageItemPreferenceAlternate image = new StorageItemPreferenceAlternate(mContext); StorageItemPreference image = new StorageItemPreference(mContext);
StorageItemPreferenceAlternate games = new StorageItemPreferenceAlternate(mContext); StorageItemPreference games = new StorageItemPreference(mContext);
StorageItemPreferenceAlternate apps = new StorageItemPreferenceAlternate(mContext); StorageItemPreference apps = new StorageItemPreference(mContext);
StorageItemPreferenceAlternate system = new StorageItemPreferenceAlternate(mContext); StorageItemPreference system = new StorageItemPreference(mContext);
StorageItemPreferenceAlternate files = new StorageItemPreferenceAlternate(mContext); StorageItemPreference files = new StorageItemPreference(mContext);
PreferenceScreen screen = mock(PreferenceScreen.class); PreferenceScreen screen = mock(PreferenceScreen.class);
when(screen.findPreference( when(screen.findPreference(
eq(StorageItemPreferenceController.AUDIO_KEY))).thenReturn(audio); eq(StorageItemPreferenceController.AUDIO_KEY))).thenReturn(audio);

View File

@@ -52,12 +52,13 @@ import org.robolectric.annotation.Config;
import java.io.File; import java.io.File;
import static com.android.settings.TestUtils.KILOBYTE;
import static com.android.settings.TestUtils.MEGABYTE;
import static com.android.settings.TestUtils.GIGABYTE;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class StorageSummaryDonutPreferenceControllerTest { public class StorageSummaryDonutPreferenceControllerTest {
private static final long KILOBYTE = 1024; // Note: When O comes around, this value changes!
private static final long MEGABYTE = KILOBYTE * KILOBYTE;
private static final long GIGABYTE = KILOBYTE * MEGABYTE;
private Context mContext; private Context mContext;
private StorageSummaryDonutPreferenceController mController; private StorageSummaryDonutPreferenceController mController;
private StorageSummaryDonutPreference mPreference; private StorageSummaryDonutPreference mPreference;