1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.example.android.support.wear.app; 18 19import android.app.Activity; 20import android.content.Intent; 21import android.os.Bundle; 22import android.view.Gravity; 23import android.view.View; 24import android.view.ViewGroup; 25import android.widget.Button; 26 27import androidx.recyclerview.widget.LinearLayoutManager; 28import androidx.recyclerview.widget.RecyclerView; 29import androidx.wear.widget.WearableRecyclerView; 30 31import com.example.android.support.wear.app.drawers.WearableDrawersDemo; 32 33import java.util.LinkedHashMap; 34import java.util.Map; 35 36/** 37 * Main activity for the wear demos. 38 */ 39public class MainDemoActivity extends Activity { 40 41 @Override 42 protected void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 45 WearableRecyclerView demoList = new WearableRecyclerView(this); 46 demoList.setPadding(30, 0, 30, 0); 47 demoList.setLayoutManager( 48 new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); 49 demoList.setAdapter(new DemoAdapter(createContentMap())); 50 demoList.setEdgeItemsCenteringEnabled(true); 51 setContentView(demoList); 52 } 53 54 private Map<String, Intent> createContentMap() { 55 Map<String, Intent> contentMap = new LinkedHashMap<>(); 56 contentMap.put("Wearable Recycler View", new Intent( 57 this, SimpleWearableRecyclerViewDemo.class)); 58 contentMap.put("Wearable Switch", new Intent( 59 this, WearableSwitchDemo.class)); 60 contentMap.put("Circular Progress Layout", new Intent( 61 this, CircularProgressLayoutDemo.class)); 62 contentMap.put("Wearable Drawers", new Intent( 63 this, WearableDrawersDemo.class)); 64 contentMap.put("Rounded Drawable", new Intent( 65 this, RoundedDrawableDemo.class)); 66 contentMap.put("Ambient Fragment", new Intent( 67 this, AmbientModeDemo.class)); 68 contentMap.put("Alert Dialog (v7)", new Intent( 69 this, AlertDialogDemo.class)); 70 contentMap.put("Confirmation Overlay", new Intent( 71 this, ConfirmationOverlayDemo.class)); 72 73 return contentMap; 74 } 75 76 private class ViewHolder extends RecyclerView.ViewHolder { 77 Button mView; 78 79 ViewHolder(Button itemView) { 80 super(itemView); 81 mView = itemView; 82 } 83 } 84 85 private class DemoAdapter extends WearableRecyclerView.Adapter<ViewHolder> { 86 private final Object[] mKeys; 87 private final Map<String, Intent> mData; 88 89 DemoAdapter(Map<String, Intent> dataMap) { 90 mKeys = dataMap.keySet().toArray(); 91 mData = dataMap; 92 } 93 94 @Override 95 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 96 Button view = new Button(parent.getContext()); 97 view.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 98 ViewGroup.LayoutParams.WRAP_CONTENT)); 99 view.setPadding(10, 10, 10, 10); 100 view.setGravity(Gravity.CENTER); 101 return new ViewHolder(view); 102 } 103 104 @Override 105 public void onBindViewHolder(ViewHolder holder, final int position) { 106 holder.mView.setText(mKeys[position].toString()); 107 holder.mView.setOnClickListener(new View.OnClickListener() { 108 @Override 109 public void onClick(View v) { 110 Intent result = new Intent(); 111 result.setClass(MainDemoActivity.this, SimpleWearableRecyclerViewDemo.class); 112 startActivity(mData.get(mKeys[position])); 113 } 114 }); 115 } 116 117 118 @Override 119 public int getItemCount() { 120 return mKeys.length; 121 } 122 } 123} 124