Skip to content

Most visited

Recently visited

navigation
WearDrawers / src / com.example.android.wearable.wear.weardrawers /

MainActivity.java

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.Fragment;
19
import android.app.FragmentManager;
20
import android.content.Context;
21
import android.graphics.drawable.Drawable;
22
import android.os.Bundle;
23
import android.support.wearable.activity.WearableActivity;
24
import android.support.wearable.view.drawer.WearableActionDrawer;
25
import android.support.wearable.view.drawer.WearableDrawerLayout;
26
import android.support.wearable.view.drawer.WearableNavigationDrawer;
27
import android.util.Log;
28
import android.view.Gravity;
29
import android.view.LayoutInflater;
30
import android.view.MenuItem;
31
import android.view.View;
32
import android.view.ViewGroup;
33
import android.view.ViewTreeObserver;
34
import android.widget.ImageView;
35
import android.widget.Toast;
36
 
37
import java.util.ArrayList;
38
 
39
/**
40
 * Demonstrates use of Navigation and Action Drawers on Android Wear.
41
 */
42
public class MainActivity extends WearableActivity implements
43
        WearableActionDrawer.OnMenuItemClickListener {
44
 
45
    private static final String TAG = "MainActivity";
46
 
47
    private WearableDrawerLayout mWearableDrawerLayout;
48
    private WearableNavigationDrawer mWearableNavigationDrawer;
49
    private WearableActionDrawer mWearableActionDrawer;
50
 
51
    private ArrayList<Planet> mSolarSystem;
52
    private int mSelectedPlanet;
53
 
54
    private PlanetFragment mPlanetFragment;
55
 
56
    @Override
57
    protected void onCreate(Bundle savedInstanceState) {
58
        super.onCreate(savedInstanceState);
59
        Log.d(TAG, "onCreate()");
60
 
61
        setContentView(R.layout.activity_main);
62
        setAmbientEnabled();
63
 
64
        mSolarSystem = initializeSolarSystem();
65
        mSelectedPlanet = 0;
66
 
67
        // Initialize content to first planet.
68
        mPlanetFragment = new PlanetFragment();
69
        Bundle args = new Bundle();
70
 
71
        int imageId = getResources().getIdentifier(mSolarSystem.get(mSelectedPlanet).getImage(),
72
                "drawable", getPackageName());
73
 
74
 
75
        args.putInt(PlanetFragment.ARG_PLANET_IMAGE_ID, imageId);
76
        mPlanetFragment.setArguments(args);
77
        FragmentManager fragmentManager = getFragmentManager();
78
        fragmentManager.beginTransaction().replace(R.id.content_frame, mPlanetFragment).commit();
79
 
80
        // Main Wearable Drawer Layout that wraps all content
81
        mWearableDrawerLayout = (WearableDrawerLayout) findViewById(R.id.drawer_layout);
82
 
83
        // Top Navigation Drawer
84
        mWearableNavigationDrawer =
85
                (WearableNavigationDrawer) findViewById(R.id.top_navigation_drawer);
86
        mWearableNavigationDrawer.setAdapter(new NavigationAdapter(this));
87
 
88
        // Bottom Action Drawer
89
        mWearableActionDrawer =
90
                (WearableActionDrawer) findViewById(R.id.bottom_action_drawer);
91
 
92
        mWearableActionDrawer.setOnMenuItemClickListener(this);
93
 
94
        // Temporarily peeks the navigation and action drawers to ensure the user is aware of them.
95
        ViewTreeObserver observer = mWearableDrawerLayout.getViewTreeObserver();
96
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
97
            @Override
98
            public void onGlobalLayout() {
99
                mWearableDrawerLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
100
                mWearableDrawerLayout.peekDrawer(Gravity.TOP);
101
                mWearableDrawerLayout.peekDrawer(Gravity.BOTTOM);
102
            }
103
        });
104
 
105
        /* Action Drawer Tip: If you only have a single action for your Action Drawer, you can use a
106
         * (custom) View to peek on top of the content by calling
107
         * mWearableActionDrawer.setPeekContent(View). Make sure you set a click listener to handle
108
         * a user clicking on your View.
109
         */
110
    }
111
 
112
    private ArrayList<Planet> initializeSolarSystem() {
113
        ArrayList<Planet> solarSystem = new ArrayList<Planet>();
114
        String[] planetArrayNames = getResources().getStringArray(R.array.planets_array_names);
115
 
116
        for (int i = 0; i < planetArrayNames.length; i++) {
117
            String planet = planetArrayNames[i];
118
            int planetResourceId =
119
                    getResources().getIdentifier(planet, "array", getPackageName());
120
            String[] planetInformation = getResources().getStringArray(planetResourceId);
121
 
122
            solarSystem.add(new Planet(
123
                    planetInformation[0],   // Name
124
                    planetInformation[1],   // Navigation icon
125
                    planetInformation[2],   // Image icon
126
                    planetInformation[3],   // Moons
127
                    planetInformation[4],   // Volume
128
                    planetInformation[5])); // Surface area
129
        }
130
 
131
        return solarSystem;
132
    }
133
 
134
    @Override
135
    public boolean onMenuItemClick(MenuItem menuItem) {
136
        Log.d(TAG, "onMenuItemClick(): " + menuItem);
137
 
138
        final int itemId = menuItem.getItemId();
139
 
140
        String toastMessage = "";
141
 
142
        switch (itemId) {
143
            case R.id.menu_planet_name:
144
                toastMessage = mSolarSystem.get(mSelectedPlanet).getName();
145
                break;
146
            case R.id.menu_number_of_moons:
147
                toastMessage = mSolarSystem.get(mSelectedPlanet).getMoons();
148
                break;
149
            case R.id.menu_volume:
150
                toastMessage = mSolarSystem.get(mSelectedPlanet).getVolume();
151
                break;
152
            case R.id.menu_surface_area:
153
                toastMessage = mSolarSystem.get(mSelectedPlanet).getSurfaceArea();
154
                break;
155
        }
156
 
157
        mWearableDrawerLayout.closeDrawer(mWearableActionDrawer);
158
 
159
        if (toastMessage.length() > 0) {
160
            Toast toast = Toast.makeText(
161
                    getApplicationContext(),
162
                    toastMessage,
163
                    Toast.LENGTH_SHORT);
164
            toast.show();
165
            return true;
166
        } else {
167
            return false;
168
        }
169
    }
170
 
171
    private final class NavigationAdapter
172
            extends WearableNavigationDrawer.WearableNavigationDrawerAdapter {
173
 
174
        private final Context mContext;
175
 
176
        public NavigationAdapter(Context context) {
177
            mContext = context;
178
        }
179
 
180
        @Override
181
        public int getCount() {
182
            return mSolarSystem.size();
183
        }
184
 
185
        @Override
186
        public void onItemSelected(int position) {
187
            Log.d(TAG, "WearableNavigationDrawerAdapter.onItemSelected(): " + position);
188
            mSelectedPlanet = position;
189
 
190
            String selectedPlanetImage = mSolarSystem.get(mSelectedPlanet).getImage();
191
            int drawableId =
192
                    getResources().getIdentifier(selectedPlanetImage, "drawable", getPackageName());
193
            mPlanetFragment.updatePlanet(drawableId);
194
        }
195
 
196
        @Override
197
        public String getItemText(int pos) {
198
            return mSolarSystem.get(pos).getName();
199
        }
200
 
201
        @Override
202
        public Drawable getItemDrawable(int pos) {
203
            String navigationIcon = mSolarSystem.get(pos).getNavigationIcon();
204
 
205
            int drawableNavigationIconId =
206
                    getResources().getIdentifier(navigationIcon, "drawable", getPackageName());
207
 
208
            return mContext.getDrawable(drawableNavigationIconId);
209
        }
210
    }
211
 
212
    /**
213
     * Fragment that appears in the "content_frame", just shows the currently selected planet.
214
     */
215
    public static class PlanetFragment extends Fragment {
216
        public static final String ARG_PLANET_IMAGE_ID = "planet_image_id";
217
 
218
        private ImageView mImageView;
219
 
220
        public PlanetFragment() {
221
            // Empty constructor required for fragment subclasses
222
        }
223
 
224
        @Override
225
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
226
                                 Bundle savedInstanceState) {
227
            View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
228
 
229
            mImageView = ((ImageView) rootView.findViewById(R.id.image));
230
 
231
            int imageIdToLoad = getArguments().getInt(ARG_PLANET_IMAGE_ID);
232
            mImageView.setImageResource(imageIdToLoad);
233
 
234
            return rootView;
235
        }
236
 
237
        public void updatePlanet(int imageId) {
238
            mImageView.setImageResource(imageId);
239
        }
240
    }
241
}
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!