diff options
Diffstat (limited to 'samples/SupportLeanbackDemos')
27 files changed, 789 insertions, 0 deletions
diff --git a/samples/SupportLeanbackDemos/Android.mk b/samples/SupportLeanbackDemos/Android.mk new file mode 100644 index 000000000..41424a709 --- /dev/null +++ b/samples/SupportLeanbackDemos/Android.mk @@ -0,0 +1,35 @@ +# 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. + +LOCAL_PATH:= $(call my-dir) + +# Build the samples. +# We need to add some special AAPT flags to generate R classes +# for resources that are included from the libraries. +include $(CLEAR_VARS) +LOCAL_PACKAGE_NAME := SupportLeanbackDemos +LOCAL_MODULE_TAGS := samples tests +LOCAL_SDK_VERSION := current +LOCAL_SRC_FILES := $(call all-java-files-under, src) +LOCAL_STATIC_JAVA_LIBRARIES := \ + android-support-v4 \ + android-support-v7-recyclerview \ + android-support-v17-leanback +LOCAL_RESOURCE_DIR = \ + $(LOCAL_PATH)/res \ + frameworks/support/v17/leanback/res +LOCAL_AAPT_FLAGS := \ + --auto-add-overlay \ + --extra-packages android.support.v17.leanback +include $(BUILD_PACKAGE) diff --git a/samples/SupportLeanbackDemos/AndroidManifest.xml b/samples/SupportLeanbackDemos/AndroidManifest.xml new file mode 100644 index 000000000..ee0707d19 --- /dev/null +++ b/samples/SupportLeanbackDemos/AndroidManifest.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="utf-8"?> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.example.android.leanback" + android:versionCode="1" + android:versionName="1.0"> + + <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="19" /> + + <application + android:label="@string/app_name" + android:icon="@drawable/ic_launcher" + android:theme="@style/Theme.Leanback"> + + <activity android:name="MainActivity" + android:label="@string/app_name"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LEANBACK_LAUNCHER" /> + </intent-filter> + </activity> + + <activity android:name="DetailsActivity" + android:exported="true" /> + + <activity android:name="VerticalGridActivity" + android:exported="true" /> + + <activity android:name="SearchActivity" + android:exported="true" /> + + </application> +</manifest> diff --git a/samples/SupportLeanbackDemos/res/drawable-hdpi/ic_launcher.png b/samples/SupportLeanbackDemos/res/drawable-hdpi/ic_launcher.png Binary files differnew file mode 100644 index 000000000..96a442e5b --- /dev/null +++ b/samples/SupportLeanbackDemos/res/drawable-hdpi/ic_launcher.png diff --git a/samples/SupportLeanbackDemos/res/drawable-ldpi/ic_launcher.png b/samples/SupportLeanbackDemos/res/drawable-ldpi/ic_launcher.png Binary files differnew file mode 100644 index 000000000..99238729d --- /dev/null +++ b/samples/SupportLeanbackDemos/res/drawable-ldpi/ic_launcher.png diff --git a/samples/SupportLeanbackDemos/res/drawable-mdpi/ic_launcher.png b/samples/SupportLeanbackDemos/res/drawable-mdpi/ic_launcher.png Binary files differnew file mode 100644 index 000000000..359047dfa --- /dev/null +++ b/samples/SupportLeanbackDemos/res/drawable-mdpi/ic_launcher.png diff --git a/samples/SupportLeanbackDemos/res/drawable-xhdpi/grid_bg.png b/samples/SupportLeanbackDemos/res/drawable-xhdpi/grid_bg.png Binary files differnew file mode 100644 index 000000000..476c698ec --- /dev/null +++ b/samples/SupportLeanbackDemos/res/drawable-xhdpi/grid_bg.png diff --git a/samples/SupportLeanbackDemos/res/drawable-xhdpi/ic_launcher.png b/samples/SupportLeanbackDemos/res/drawable-xhdpi/ic_launcher.png Binary files differnew file mode 100644 index 000000000..71c6d760f --- /dev/null +++ b/samples/SupportLeanbackDemos/res/drawable-xhdpi/ic_launcher.png diff --git a/samples/SupportLeanbackDemos/res/drawable/details_img.png b/samples/SupportLeanbackDemos/res/drawable/details_img.png Binary files differnew file mode 100644 index 000000000..7ea688b64 --- /dev/null +++ b/samples/SupportLeanbackDemos/res/drawable/details_img.png diff --git a/samples/SupportLeanbackDemos/res/drawable/ic_action_a.png b/samples/SupportLeanbackDemos/res/drawable/ic_action_a.png Binary files differnew file mode 100644 index 000000000..3d555efa5 --- /dev/null +++ b/samples/SupportLeanbackDemos/res/drawable/ic_action_a.png diff --git a/samples/SupportLeanbackDemos/res/drawable/ic_title.png b/samples/SupportLeanbackDemos/res/drawable/ic_title.png Binary files differnew file mode 100644 index 000000000..1c62b2e18 --- /dev/null +++ b/samples/SupportLeanbackDemos/res/drawable/ic_title.png diff --git a/samples/SupportLeanbackDemos/res/drawable/text_bg.xml b/samples/SupportLeanbackDemos/res/drawable/text_bg.xml new file mode 100644 index 000000000..c67924c5b --- /dev/null +++ b/samples/SupportLeanbackDemos/res/drawable/text_bg.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. +--> + +<shape xmlns:android="http://schemas.android.com/apk/res/android" + android:shape="rectangle"> + <gradient + android:startColor="#FFFF0000" + android:endColor="#80FF00FF" + android:angle="45" /> + <padding + android:left="7dp" + android:top="7dp" + android:right="7dp" + android:bottom="7dp" /> +</shape> diff --git a/samples/SupportLeanbackDemos/res/layout/details.xml b/samples/SupportLeanbackDemos/res/layout/details.xml new file mode 100644 index 000000000..4af4e6a1e --- /dev/null +++ b/samples/SupportLeanbackDemos/res/layout/details.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. +--> + +<fragment xmlns:android="http://schemas.android.com/apk/res/android" + android:name="com.example.android.leanback.DetailsFragment" + android:id="@+id/details_fragment" + android:layout_width="match_parent" + android:layout_height="match_parent" +/> diff --git a/samples/SupportLeanbackDemos/res/layout/main.xml b/samples/SupportLeanbackDemos/res/layout/main.xml new file mode 100644 index 000000000..b6b08b3b5 --- /dev/null +++ b/samples/SupportLeanbackDemos/res/layout/main.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. +--> + +<fragment xmlns:android="http://schemas.android.com/apk/res/android" + android:name="com.example.android.leanback.BrowseFragment" + android:id="@+id/main_browse_fragment" + android:layout_width="match_parent" + android:layout_height="match_parent" +/> diff --git a/samples/SupportLeanbackDemos/res/layout/search.xml b/samples/SupportLeanbackDemos/res/layout/search.xml new file mode 100644 index 000000000..b65600ca5 --- /dev/null +++ b/samples/SupportLeanbackDemos/res/layout/search.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. +--> + +<fragment xmlns:android="http://schemas.android.com/apk/res/android" + android:name="com.example.android.leanback.SearchFragment" + android:id="@+id/search_fragment" + android:layout_width="match_parent" + android:layout_height="match_parent" + />
\ No newline at end of file diff --git a/samples/SupportLeanbackDemos/res/layout/vertical_grid.xml b/samples/SupportLeanbackDemos/res/layout/vertical_grid.xml new file mode 100644 index 000000000..1007042c4 --- /dev/null +++ b/samples/SupportLeanbackDemos/res/layout/vertical_grid.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. +--> + +<fragment xmlns:android="http://schemas.android.com/apk/res/android" + android:name="com.example.android.leanback.VerticalGridFragment" + android:id="@+id/vertical_grid_fragment" + android:layout_width="match_parent" + android:layout_height="match_parent" +/> diff --git a/samples/SupportLeanbackDemos/res/values/strings.xml b/samples/SupportLeanbackDemos/res/values/strings.xml new file mode 100644 index 000000000..e8840436b --- /dev/null +++ b/samples/SupportLeanbackDemos/res/values/strings.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. +--> + +<resources> + <string name="app_name">MainActivity</string> +</resources> diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java new file mode 100644 index 000000000..2345ed09b --- /dev/null +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java @@ -0,0 +1,78 @@ +/* + * 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.content.Intent; +import android.os.Bundle; +import android.support.v17.leanback.widget.ArrayObjectAdapter; +import android.support.v17.leanback.widget.HeaderItem; +import android.support.v17.leanback.widget.ListRow; +import android.support.v17.leanback.widget.ListRowPresenter; +import android.support.v17.leanback.widget.OnItemClickedListener; +import android.support.v17.leanback.widget.Row; +import android.util.Log; +import android.view.View; + +public class BrowseFragment extends android.support.v17.leanback.app.BrowseFragment { + private static final String TAG = "leanback.BrowseFragment"; + + private static final int NUM_ROWS = 3; + private ArrayObjectAdapter mRowsAdapter; + + @Override + public void onCreate(Bundle savedInstanceState) { + Log.i(TAG, "onCreate"); + super.onCreate(savedInstanceState); + + Params p = new Params(); + p.setBadgeImage(getActivity().getResources().getDrawable(R.drawable.ic_title)); + p.setTitle("Leanback Sample App"); + p.setHeadersState(HEADERS_ENABLED); + setBrowseParams(p); + + setOnSearchClickedListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(getActivity(), SearchActivity.class); + startActivity(intent); + } + }); + + setupRows(); + setOnItemClickedListener(new ItemClickedListener()); + } + + private void setupRows() { + mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter()); + + for (int i = 0; i < NUM_ROWS; ++i) { + ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new CardPresenter()); + listRowAdapter.add("Hello world"); + listRowAdapter.add("This is a test"); + HeaderItem header = new HeaderItem(i, "Row " + i, null); + mRowsAdapter.add(new ListRow(header, listRowAdapter)); + } + + setAdapter(mRowsAdapter); + } + + private final class ItemClickedListener implements OnItemClickedListener { + public void onItemClicked(Object item, Row row) { + // TODO: use a fragment transaction instead of launching a new + // activity + Intent intent = new Intent(getActivity(), DetailsActivity.class); + startActivity(intent); + } + } +} diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/CardPresenter.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/CardPresenter.java new file mode 100644 index 000000000..502c77aea --- /dev/null +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/CardPresenter.java @@ -0,0 +1,43 @@ +/* + * 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.support.v17.leanback.widget.ImageCardView; +import android.support.v17.leanback.widget.Presenter; +import android.util.Log; +import android.view.ViewGroup; +import android.widget.TextView; + +public class CardPresenter extends Presenter { + private static final String TAG = "CardPresenter"; + + public ViewHolder onCreateViewHolder(ViewGroup parent) { + Log.d(TAG, "onCreateViewHolder"); + ImageCardView v = new ImageCardView(parent.getContext()); + v.setFocusable(true); + v.setFocusableInTouchMode(true); + v.setMainImage( + parent.getContext().getResources().getDrawable(R.drawable.text_bg)); + return new ViewHolder(v); + } + + public void onBindViewHolder(ViewHolder viewHolder, Object item) { + Log.d(TAG, "onBindViewHolder for " + item.toString()); + ((ImageCardView) viewHolder.view).setTitleText(item.toString()); + } + + public void onUnbindViewHolder(ViewHolder viewHolder) { + Log.d(TAG, "onUnbindViewHolder"); + } +} diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/DetailsActivity.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/DetailsActivity.java new file mode 100644 index 000000000..082b134f5 --- /dev/null +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/DetailsActivity.java @@ -0,0 +1,28 @@ +/* + * 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 DetailsActivity extends Activity +{ + /** Called when the activity is first created. */ + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.details); + } +} diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/DetailsDescriptionPresenter.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/DetailsDescriptionPresenter.java new file mode 100644 index 000000000..6d376f0aa --- /dev/null +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/DetailsDescriptionPresenter.java @@ -0,0 +1,32 @@ +/* + * 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.support.v17.leanback.widget.AbstractDetailsDescriptionPresenter; +import android.support.v17.leanback.widget.DetailsOverviewRow; + +public class DetailsDescriptionPresenter extends AbstractDetailsDescriptionPresenter { + + @Override + protected void onBindDescription(ViewHolder vh, Object item) { + vh.getTitle().setText(item.toString()); + vh.getSubtitle().setText("2013 - 2014 Drama TV-14"); + vh.getBody().setText("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do " + + "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " + + "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo " + + "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse " + + "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non " + + "proident, sunt in culpa qui officia deserunt mollit anim id est laborum."); + } +} diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/DetailsFragment.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/DetailsFragment.java new file mode 100644 index 000000000..c9a85f39f --- /dev/null +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/DetailsFragment.java @@ -0,0 +1,77 @@ +/* + * 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.content.res.Resources; +import android.os.Bundle; +import android.support.v17.leanback.widget.Action; +import android.support.v17.leanback.widget.ArrayObjectAdapter; +import android.support.v17.leanback.widget.ClassPresenterSelector; +import android.support.v17.leanback.widget.DetailsOverviewRow; +import android.support.v17.leanback.widget.DetailsOverviewRowPresenter; +import android.support.v17.leanback.widget.HeaderItem; +import android.support.v17.leanback.widget.ListRow; +import android.support.v17.leanback.widget.ListRowPresenter; +import android.support.v17.leanback.widget.OnActionClickedListener; +import android.util.Log; +import android.widget.Toast; + +public class DetailsFragment extends android.support.v17.leanback.app.DetailsFragment { + private static final String TAG = "leanback.BrowseFragment"; + + private static final int NUM_ROWS = 3; + private ArrayObjectAdapter mRowsAdapter; + + @Override + public void onCreate(Bundle savedInstanceState) { + Log.i(TAG, "onCreate"); + super.onCreate(savedInstanceState); + + setupRows(); + } + + private void setupRows() { + ClassPresenterSelector ps = new ClassPresenterSelector(); + DetailsOverviewRowPresenter dorPresenter = + new DetailsOverviewRowPresenter(new DetailsDescriptionPresenter()); + dorPresenter.setOnActionClickedListener(new OnActionClickedListener() { + public void onActionClicked(Action action) { + Toast.makeText(getActivity(), action.toString(), Toast.LENGTH_SHORT).show(); + } + }); + + ps.addClassPresenter(DetailsOverviewRow.class, dorPresenter); + ps.addClassPresenter(ListRow.class, + new ListRowPresenter()); + mRowsAdapter = new ArrayObjectAdapter(ps); + + Resources res = getActivity().getResources(); + DetailsOverviewRow dor = new DetailsOverviewRow("Details Overview"); + dor.setImageDrawable(res.getDrawable(R.drawable.details_img)); + dor.addAction(new Action(1, "Buy $9.99")); + dor.addAction(new Action(2, "Rent", "$3.99", res.getDrawable(R.drawable.ic_action_a))); + mRowsAdapter.add(dor); + + for (int i = 0; i < NUM_ROWS; ++i) { + ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new StringPresenter()); + listRowAdapter.add("Hello world"); + listRowAdapter.add("This is a test"); + HeaderItem header = new HeaderItem(i, "Row " + i, null); + mRowsAdapter.add(new ListRow(header, listRowAdapter)); + } + + setAdapter(mRowsAdapter); + } + +} diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/MainActivity.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/MainActivity.java new file mode 100644 index 000000000..74e7fb216 --- /dev/null +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/MainActivity.java @@ -0,0 +1,28 @@ +/* + * 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 MainActivity extends Activity +{ + /** Called when the activity is first created. */ + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + } +} diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/SearchActivity.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/SearchActivity.java new file mode 100644 index 000000000..437bd7267 --- /dev/null +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/SearchActivity.java @@ -0,0 +1,28 @@ +/* + * 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 SearchActivity extends Activity +{ + /** Called when the activity is first created. */ + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.search); + } +} diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/SearchFragment.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/SearchFragment.java new file mode 100644 index 000000000..0489196b0 --- /dev/null +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/SearchFragment.java @@ -0,0 +1,85 @@ +package com.example.android.leanback; + +import android.content.Intent; +import android.os.Bundle; +import android.os.Handler; +import android.support.v17.leanback.widget.ArrayObjectAdapter; +import android.support.v17.leanback.widget.HeaderItem; +import android.support.v17.leanback.widget.ListRow; +import android.support.v17.leanback.widget.ListRowPresenter; +import android.support.v17.leanback.widget.ObjectAdapter; +import android.support.v17.leanback.widget.OnItemClickedListener; +import android.support.v17.leanback.widget.Row; +import android.text.TextUtils; +import android.util.Log; + +public class SearchFragment extends android.support.v17.leanback.app.SearchFragment + implements android.support.v17.leanback.app.SearchFragment.SearchResultProvider { + private static final String TAG = "leanback.SearchFragment"; + private static final int NUM_ROWS = 3; + private static final int SEARCH_DELAY_MS = 300; + + private ArrayObjectAdapter mRowsAdapter; + private Handler mHandler = new Handler(); + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter()); + setSearchResultProvider(this); + setOnItemClickedListener(new ItemClickedListener()); + } + + @Override + public ObjectAdapter getResultsAdapter() { + return mRowsAdapter; + } + + @Override + public boolean onQueryTextChange(String newQuery) { + Log.i(TAG, String.format("Search Query Text Change %s", newQuery)); + mRowsAdapter.clear(); + if (!TextUtils.isEmpty(newQuery)) { + mHandler.removeCallbacks(mDelayedLoad); + mHandler.postDelayed(mDelayedLoad, SEARCH_DELAY_MS); + } + return true; + } + + @Override + public boolean onQueryTextSubmit(String query) { + Log.i(TAG, String.format("Search Query Text Submit %s", query)); + mRowsAdapter.clear(); + if (!TextUtils.isEmpty(query)) { + mHandler.removeCallbacks(mDelayedLoad); + mHandler.postDelayed(mDelayedLoad, SEARCH_DELAY_MS); + } + return true; + } + + private void loadRows() { + for (int i = 0; i < NUM_ROWS; ++i) { + ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new StringPresenter()); + listRowAdapter.add("Hello world"); + listRowAdapter.add("This is a test"); + HeaderItem header = new HeaderItem(i, "Row " + i, null); + mRowsAdapter.add(new ListRow(header, listRowAdapter)); + } + } + + private Runnable mDelayedLoad = new Runnable() { + @Override + public void run() { + loadRows(); + } + }; + private final class ItemClickedListener implements OnItemClickedListener { + public void onItemClicked(Object item, Row row) { + // TODO: use a fragment transaction instead of launching a new + // activity + Intent intent = new Intent(getActivity(), DetailsActivity.class); + startActivity(intent); + } + } +} diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/StringPresenter.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/StringPresenter.java new file mode 100644 index 000000000..5c80e0be4 --- /dev/null +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/StringPresenter.java @@ -0,0 +1,42 @@ +/* + * 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.support.v17.leanback.widget.Presenter; +import android.util.Log; +import android.view.ViewGroup; +import android.widget.TextView; + +public class StringPresenter extends Presenter { + private static final String TAG = "StringPresenter"; + + public ViewHolder onCreateViewHolder(ViewGroup parent) { + Log.d(TAG, "onCreateViewHolder"); + TextView tv = new TextView(parent.getContext()); + tv.setFocusable(true); + tv.setFocusableInTouchMode(true); + tv.setBackground( + parent.getContext().getResources().getDrawable(R.drawable.text_bg)); + return new ViewHolder(tv); + } + + public void onBindViewHolder(ViewHolder viewHolder, Object item) { + Log.d(TAG, "onBindViewHolder for " + item.toString()); + ((TextView) viewHolder.view).setText(item.toString()); + } + + public void onUnbindViewHolder(ViewHolder viewHolder) { + Log.d(TAG, "onUnbindViewHolder"); + } +} diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridActivity.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridActivity.java new file mode 100644 index 000000000..c5262b99d --- /dev/null +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridActivity.java @@ -0,0 +1,29 @@ +/* + * 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 VerticalGridActivity extends Activity +{ + /** Called when the activity is first created. */ + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.vertical_grid); + getWindow().setBackgroundDrawableResource(R.drawable.grid_bg); + } +} diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridFragment.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridFragment.java new file mode 100644 index 000000000..495bebeb5 --- /dev/null +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/VerticalGridFragment.java @@ -0,0 +1,111 @@ +/* + * 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.content.res.Resources; +import android.graphics.Color; +import android.os.Bundle; +import android.support.v17.leanback.widget.ArrayObjectAdapter; +import android.support.v17.leanback.widget.Presenter; +import android.support.v17.leanback.widget.VerticalGridPresenter; +import android.support.v17.leanback.widget.Row; +import android.support.v17.leanback.widget.OnItemClickedListener; +import android.support.v17.leanback.widget.OnItemSelectedListener; +import android.util.Log; +import android.view.Gravity; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import java.util.Random; + +public class VerticalGridFragment extends android.support.v17.leanback.app.VerticalGridFragment { + private static final String TAG = "leanback.VerticalGridFragment"; + + private static final int NUM_COLUMNS = 3; + private static final int NUM_ITEMS = 50; + private static final int HEIGHT = 200; + + private ArrayObjectAdapter mAdapter; + private Random mRandom; + + @Override + public void onCreate(Bundle savedInstanceState) { + Log.i(TAG, "onCreate"); + super.onCreate(savedInstanceState); + + mRandom = new Random(); + + Params p = new Params(); + p.setBadgeImage(getActivity().getResources().getDrawable(R.drawable.ic_title)); + p.setTitle("Leanback Vertical Grid Demo"); + setParams(p); + + setupFragment(); + } + + private void setupFragment() { + VerticalGridPresenter gridPresenter = new VerticalGridPresenter(); + gridPresenter.setNumberOfColumns(NUM_COLUMNS); + setGridPresenter(gridPresenter); + + mAdapter = new ArrayObjectAdapter(new GridItemPresenter()); + for (int i = 0; i < NUM_ITEMS; i++) { + mAdapter.add(new MyItem(i)); + } + setAdapter(mAdapter); + + setOnItemSelectedListener(new OnItemSelectedListener() { + @Override + public void onItemSelected(Object item, Row row) { + Log.i(TAG, "item selected: " + ((MyItem) item).id); + } + }); + + setOnItemClickedListener(new OnItemClickedListener() { + @Override + public void onItemClicked(Object item, Row row) { + Log.i(TAG, "item clicked: " + ((MyItem) item).id); + } + }); + } + + private class GridItemPresenter extends Presenter { + public ViewHolder onCreateViewHolder(ViewGroup parent) { + TextView view = new TextView(parent.getContext()); + // Choose a random height between HEIGHT and 1.5 * HEIGHT to + // demonstrate the staggered nature of the grid. + final int height = HEIGHT + mRandom.nextInt(HEIGHT / 2); + view.setLayoutParams(new ViewGroup.LayoutParams(200, height)); + view.setFocusable(true); + view.setFocusableInTouchMode(true); + view.setBackgroundColor(Color.DKGRAY); + view.setGravity(Gravity.CENTER); + return new ViewHolder(view); + } + + public void onBindViewHolder(ViewHolder viewHolder, Object item) { + ((TextView) viewHolder.view).setText(Integer.toString(((MyItem) item).id)); + } + + public void onUnbindViewHolder(ViewHolder viewHolder) {} + } + + static class MyItem { + int id; + MyItem(int id) { + this.id = id; + } + } +} |