1 /* 2 Copyright 2016 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 package com.example.android.wearable.wear.weardrawers; 17 18 import android.app.Activity; 19 import android.app.Fragment; 20 import android.app.FragmentManager; 21 import android.content.Context; 22 import android.graphics.ColorFilter; 23 import android.graphics.ColorMatrix; 24 import android.graphics.ColorMatrixColorFilter; 25 import android.graphics.drawable.Drawable; 26 import android.os.Bundle; 27 import android.support.wear.ambient.AmbientMode; 28 import android.support.wear.widget.drawer.WearableActionDrawerView; 29 import android.support.wear.widget.drawer.WearableNavigationDrawerView; 30 import android.util.Log; 31 import android.view.LayoutInflater; 32 import android.view.MenuItem; 33 import android.view.View; 34 import android.view.ViewGroup; 35 import android.widget.ImageView; 36 import android.widget.Toast; 37 38 import java.util.ArrayList; 39 40 /** 41 * Demonstrates use of Navigation and Action Drawers on Android Wear. 42 */ 43 public class MainActivity extends Activity implements 44 AmbientMode.AmbientCallbackProvider, 45 MenuItem.OnMenuItemClickListener, 46 WearableNavigationDrawerView.OnItemSelectedListener { 47 48 private static final String TAG = "MainActivity"; 49 50 private WearableNavigationDrawerView mWearableNavigationDrawer; 51 private WearableActionDrawerView mWearableActionDrawer; 52 53 private ArrayList<Planet> mSolarSystem; 54 private int mSelectedPlanet; 55 56 private PlanetFragment mPlanetFragment; 57 58 @Override 59 protected void onCreate(Bundle savedInstanceState) { 60 super.onCreate(savedInstanceState); 61 Log.d(TAG, "onCreate()"); 62 63 setContentView(R.layout.activity_main); 64 65 // Enables Ambient mode. 66 AmbientMode.attachAmbientSupport(this); 67 68 mSolarSystem = initializeSolarSystem(); 69 mSelectedPlanet = 0; 70 71 // Initialize content to first planet. 72 mPlanetFragment = new PlanetFragment(); 73 Bundle args = new Bundle(); 74 75 int imageId = getResources().getIdentifier(mSolarSystem.get(mSelectedPlanet).getImage(), 76 "drawable", getPackageName()); 77 78 79 args.putInt(PlanetFragment.ARG_PLANET_IMAGE_ID, imageId); 80 mPlanetFragment.setArguments(args); 81 FragmentManager fragmentManager = getFragmentManager(); 82 fragmentManager.beginTransaction().replace(R.id.content_frame, mPlanetFragment).commit(); 83 84 85 // Top Navigation Drawer 86 mWearableNavigationDrawer = 87 (WearableNavigationDrawerView) findViewById(R.id.top_navigation_drawer); 88 mWearableNavigationDrawer.setAdapter(new NavigationAdapter(this)); 89 // Peeks navigation drawer on the top. 90 mWearableNavigationDrawer.getController().peekDrawer(); 91 mWearableNavigationDrawer.addOnItemSelectedListener(this); 92 93 // Bottom Action Drawer 94 mWearableActionDrawer = 95 (WearableActionDrawerView) findViewById(R.id.bottom_action_drawer); 96 // Peeks action drawer on the bottom. 97 mWearableActionDrawer.getController().peekDrawer(); 98 mWearableActionDrawer.setOnMenuItemClickListener(this); 99 100 /* Action Drawer Tip: If you only have a single action for your Action Drawer, you can use a 101 * (custom) View to peek on top of the content by calling 102 * mWearableActionDrawer.setPeekContent(View). Make sure you set a click listener to handle 103 * a user clicking on your View. 104 */ 105 } 106 107 private ArrayList<Planet> initializeSolarSystem() { 108 ArrayList<Planet> solarSystem = new ArrayList<Planet>(); 109 String[] planetArrayNames = getResources().getStringArray(R.array.planets_array_names); 110 111 for (int i = 0; i < planetArrayNames.length; i++) { 112 String planet = planetArrayNames[i]; 113 int planetResourceId = 114 getResources().getIdentifier(planet, "array", getPackageName()); 115 String[] planetInformation = getResources().getStringArray(planetResourceId); 116 117 solarSystem.add(new Planet( 118 planetInformation[0], // Name 119 planetInformation[1], // Navigation icon 120 planetInformation[2], // Image icon 121 planetInformation[3], // Moons 122 planetInformation[4], // Volume 123 planetInformation[5])); // Surface area 124 } 125 126 return solarSystem; 127 } 128 129 @Override 130 public boolean onMenuItemClick(MenuItem menuItem) { 131 Log.d(TAG, "onMenuItemClick(): " + menuItem); 132 133 final int itemId = menuItem.getItemId(); 134 135 String toastMessage = ""; 136 137 switch (itemId) { 138 case R.id.menu_planet_name: 139 toastMessage = mSolarSystem.get(mSelectedPlanet).getName(); 140 break; 141 case R.id.menu_number_of_moons: 142 toastMessage = mSolarSystem.get(mSelectedPlanet).getMoons(); 143 break; 144 case R.id.menu_volume: 145 toastMessage = mSolarSystem.get(mSelectedPlanet).getVolume(); 146 break; 147 case R.id.menu_surface_area: 148 toastMessage = mSolarSystem.get(mSelectedPlanet).getSurfaceArea(); 149 break; 150 } 151 152 mWearableActionDrawer.getController().closeDrawer(); 153 154 if (toastMessage.length() > 0) { 155 Toast toast = Toast.makeText( 156 getApplicationContext(), 157 toastMessage, 158 Toast.LENGTH_SHORT); 159 toast.show(); 160 return true; 161 } else { 162 return false; 163 } 164 } 165 166 // Updates content when user changes between items in the navigation drawer. 167 @Override 168 public void onItemSelected(int position) { 169 Log.d(TAG, "WearableNavigationDrawerView triggered onItemSelected(): " + position); 170 mSelectedPlanet = position; 171 172 String selectedPlanetImage = mSolarSystem.get(mSelectedPlanet).getImage(); 173 int drawableId = 174 getResources().getIdentifier(selectedPlanetImage, "drawable", getPackageName()); 175 mPlanetFragment.updatePlanet(drawableId); 176 } 177 178 private final class NavigationAdapter 179 extends WearableNavigationDrawerView.WearableNavigationDrawerAdapter { 180 181 private final Context mContext; 182 183 public NavigationAdapter(Context context) { 184 mContext = context; 185 } 186 187 @Override 188 public int getCount() { 189 return mSolarSystem.size(); 190 } 191 192 @Override 193 public String getItemText(int pos) { 194 return mSolarSystem.get(pos).getName(); 195 } 196 197 @Override 198 public Drawable getItemDrawable(int pos) { 199 String navigationIcon = mSolarSystem.get(pos).getNavigationIcon(); 200 201 int drawableNavigationIconId = 202 getResources().getIdentifier(navigationIcon, "drawable", getPackageName()); 203 204 return mContext.getDrawable(drawableNavigationIconId); 205 } 206 } 207 208 /** 209 * Fragment that appears in the "content_frame", just shows the currently selected planet. 210 */ 211 public static class PlanetFragment extends Fragment { 212 public static final String ARG_PLANET_IMAGE_ID = "planet_image_id"; 213 214 private ImageView mImageView; 215 private ColorFilter mImageViewColorFilter; 216 217 public PlanetFragment() { 218 // Empty constructor required for fragment subclasses 219 } 220 221 @Override 222 public View onCreateView( 223 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 224 View rootView = inflater.inflate(R.layout.fragment_planet, container, false); 225 226 mImageView = ((ImageView) rootView.findViewById(R.id.image)); 227 228 int imageIdToLoad = getArguments().getInt(ARG_PLANET_IMAGE_ID); 229 mImageView.setImageResource(imageIdToLoad); 230 231 mImageViewColorFilter = mImageView.getColorFilter(); 232 233 return rootView; 234 } 235 236 public void updatePlanet(int imageId) { 237 mImageView.setImageResource(imageId); 238 } 239 240 public void onEnterAmbientInFragment(Bundle ambientDetails) { 241 Log.d(TAG, "PlanetFragment.onEnterAmbient() " + ambientDetails); 242 243 // Convert image to grayscale for ambient mode. 244 ColorMatrix matrix = new ColorMatrix(); 245 matrix.setSaturation(0); 246 247 ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix); 248 mImageView.setColorFilter(filter); 249 } 250 251 /** Restores the UI to active (non-ambient) mode. */ 252 public void onExitAmbientInFragment() { 253 Log.d(TAG, "PlanetFragment.onExitAmbient()"); 254 255 mImageView.setColorFilter(mImageViewColorFilter); 256 } 257 } 258 259 @Override 260 public AmbientMode.AmbientCallback getAmbientCallback() { 261 return new MyAmbientCallback(); 262 } 263 264 private class MyAmbientCallback extends AmbientMode.AmbientCallback { 265 /** Prepares the UI for ambient mode. */ 266 @Override 267 public void onEnterAmbient(Bundle ambientDetails) { 268 super.onEnterAmbient(ambientDetails); 269 Log.d(TAG, "onEnterAmbient() " + ambientDetails); 270 271 mPlanetFragment.onEnterAmbientInFragment(ambientDetails); 272 mWearableNavigationDrawer.getController().closeDrawer(); 273 mWearableActionDrawer.getController().closeDrawer(); 274 } 275 276 /** Restores the UI to active (non-ambient) mode. */ 277 @Override 278 public void onExitAmbient() { 279 super.onExitAmbient(); 280 Log.d(TAG, "onExitAmbient()"); 281 282 mPlanetFragment.onExitAmbientInFragment(); 283 mWearableActionDrawer.getController().peekDrawer(); 284 } 285 } 286 }