Merge "Add demos for GuidedStepFragment." into lmp-mr1-ub-dev

This commit is contained in:
Kris Giesing
2015-03-19 16:41:23 +00:00
committed by Android (Google) Code Review
17 changed files with 415 additions and 17 deletions

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2014 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.example.android.leanback;
import android.app.Activity;
import android.os.Bundle;
public class BrowseActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browse);
}
}

View File

@@ -34,7 +34,7 @@ public class BrowseErrorActivity extends Activity
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setContentView(R.layout.browse);
testError();
}

View File

@@ -0,0 +1,190 @@
/*
* Copyright (C) 2014 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.example.android.leanback;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v17.leanback.app.GuidedStepFragment;
import android.support.v17.leanback.widget.GuidedAction;
import android.support.v17.leanback.widget.GuidanceStylist;
import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
import java.util.List;
import java.util.ArrayList;
/**
* Activity that showcases different aspects of GuidedStepFragments.
*/
public class GuidedStepActivity extends Activity {
private static final int CONTINUE = 0;
private static final int BACK = 1;
private static final int OPTION_CHECK_SET_ID = 10;
private static final int DEFAULT_OPTION = 0;
private static final String[] OPTION_NAMES = { "Option A", "Option B", "Option C" };
private static final String[] OPTION_DESCRIPTIONS = { "Here's one thing you can do",
"Here's another thing you can do", "Here's one more thing you can do" };
private static final int[] OPTION_DRAWABLES = { R.drawable.ic_guidedstep_option_a,
R.drawable.ic_guidedstep_option_b, R.drawable.ic_guidedstep_option_c };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GuidedStepFragment.add(getFragmentManager(), new FirstStepFragment());
}
private static void addAction(List<GuidedAction> actions, long id, String title, String desc) {
actions.add(new GuidedAction.Builder()
.id(id)
.title(title)
.description(desc)
.build());
}
private static void addCheckedAction(List<GuidedAction> actions, int iconResId, Context context,
String title, String desc) {
actions.add(new GuidedAction.Builder()
.title(title)
.description(desc)
.checkSetId(OPTION_CHECK_SET_ID)
.iconResourceId(iconResId, context)
.build());
}
/**
* The first fragment is instantiated via XML, so it must be public.
*/
public static class FirstStepFragment extends GuidedStepFragment {
@Override
public int onProvideTheme() {
return R.style.Theme_Example_Leanback_GuidedStep_First;
}
@Override
public Guidance onCreateGuidance(Bundle savedInstanceState) {
String title = getString(R.string.guidedstep_first_title);
String breadcrumb = getString(R.string.guidedstep_first_breadcrumb);
String description = getString(R.string.guidedstep_first_description);
Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon);
return new Guidance(title, description, breadcrumb, icon);
}
@Override
public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
addAction(actions, CONTINUE, "Continue", "Let's do it");
addAction(actions, BACK, "Cancel", "Nevermind");
}
@Override
public void onGuidedActionClicked(GuidedAction action) {
FragmentManager fm = getFragmentManager();
if (action.getId() == CONTINUE) {
GuidedStepFragment.add(fm, new SecondStepFragment());
} else {
getActivity().finish();
}
}
}
private static class SecondStepFragment extends GuidedStepFragment {
@Override
public Guidance onCreateGuidance(Bundle savedInstanceState) {
String title = getString(R.string.guidedstep_second_title);
String breadcrumb = getString(R.string.guidedstep_second_breadcrumb);
String description = getString(R.string.guidedstep_second_description);
Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon);
return new Guidance(title, description, breadcrumb, icon);
}
@Override
public GuidanceStylist onCreateGuidanceStylist() {
return new GuidanceStylist() {
@Override
public int onProvideLayoutId() {
return R.layout.guidedstep_second_guidance;
}
};
}
@Override
public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
String desc = "The description can be quite long as well. ";
desc += "Just be sure to set multilineDescription to true in the GuidedAction.";
actions.add(new GuidedAction.Builder()
.title("Note that Guided Actions can have titles that are quite long.")
.description(desc)
.multilineDescription(true)
.infoOnly(true)
.enabled(false)
.build());
for (int i = 0; i < OPTION_NAMES.length; i++) {
addCheckedAction(actions, OPTION_DRAWABLES[i], getActivity(), OPTION_NAMES[i],
OPTION_DESCRIPTIONS[i]);
if (i == DEFAULT_OPTION) {
actions.get(actions.size() -1).setChecked(true);
}
}
}
@Override
public void onGuidedActionClicked(GuidedAction action) {
FragmentManager fm = getFragmentManager();
GuidedStepFragment.add(fm, new ThirdStepFragment(getSelectedActionPosition()-1));
}
}
private static class ThirdStepFragment extends GuidedStepFragment {
private final int mOption;
public ThirdStepFragment(int option) {
mOption = option;
}
@Override
public Guidance onCreateGuidance(Bundle savedInstanceState) {
String title = getString(R.string.guidedstep_third_title);
String breadcrumb = getString(R.string.guidedstep_third_breadcrumb);
String description = "You chose: " + OPTION_NAMES[mOption];
Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon);
return new Guidance(title, description, breadcrumb, icon);
}
@Override
public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
addAction(actions, CONTINUE, "Done", "All finished");
addAction(actions, BACK, "Back", "Forgot something...");
}
@Override
public void onGuidedActionClicked(GuidedAction action) {
if (action.getId() == CONTINUE) {
getActivity().finish();
} else {
getFragmentManager().popBackStack();
}
}
}
}

View File

@@ -1,28 +1,92 @@
/*
* Copyright (C) 2014 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
* 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
* 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.
* 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.example.android.leanback;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v17.leanback.app.GuidedStepFragment;
import android.support.v17.leanback.widget.GuidedAction;
import android.support.v17.leanback.widget.GuidanceStylist.Guidance;
import java.util.List;
import java.util.ArrayList;
/**
* Activity that allows navigation among the demo activities.
*/
public class MainActivity extends Activity {
private GuidedStepFragment mGuidedStepFragment;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mGuidedStepFragment = new StepFragment();
GuidedStepFragment.add(getFragmentManager(), mGuidedStepFragment);
}
private static class StepFragment extends GuidedStepFragment {
@Override
public Guidance onCreateGuidance(Bundle savedInstanceState) {
String title = getString(R.string.main_title);
String breadcrumb = getString(R.string.main_breadcrumb);
String description = "";
Drawable icon = getActivity().getDrawable(R.drawable.ic_main_icon);
return new Guidance(title, description, breadcrumb, icon);
}
@Override
public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
addAction(actions, BrowseActivity.class, R.string.browse, R.string.browse_description);
addAction(actions, SearchActivity.class, R.string.search, R.string.search_description);
addAction(actions, DetailsActivity.class, R.string.details, R.string.details_description);
actions.get(actions.size()-1).getIntent().putExtra(DetailsActivity.EXTRA_ITEM,
new PhotoItem("Hello world", R.drawable.gallery_photo_1));
addAction(actions, PlaybackOverlayActivity.class, R.string.playback,
R.string.playback_description);
addAction(actions, HorizontalGridTestActivity.class, R.string.hgrid,
R.string.hgrid_description);
addAction(actions, VerticalGridActivity.class, R.string.vgrid,
R.string.vgrid_description);
addAction(actions, GuidedStepActivity.class, R.string.guidedstep,
R.string.guidedstep_description);
addAction(actions, BrowseErrorActivity.class, R.string.browseerror,
R.string.browseerror_description);
}
private void addAction(List<GuidedAction> actions, Class cls, int titleRes, int descRes) {
actions.add(new GuidedAction.Builder()
.intent(new Intent(getActivity(), cls))
.title(getString(titleRes))
.description(getString(descRes))
.build());
}
@Override
public void onGuidedActionClicked(GuidedAction action) {
Intent intent = action.getIntent();
if (intent != null) {
startActivity(intent);
}
}
}
}