1 /*
2  * Copyright (C) 2014 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 
17 package com.example.android.google.wearable.app;
18 
19 import android.app.Activity;
20 import android.app.Fragment;
21 import android.app.FragmentManager;
22 import android.content.Context;
23 import android.graphics.Bitmap;
24 import android.graphics.Canvas;
25 import android.graphics.Color;
26 import android.graphics.Paint;
27 import android.graphics.Point;
28 import android.graphics.Typeface;
29 import android.graphics.Paint.Align;
30 import android.graphics.drawable.BitmapDrawable;
31 import android.graphics.drawable.Drawable;
32 import android.os.Bundle;
33 import android.support.wearable.view.CardFragment;
34 import android.support.wearable.view.FragmentGridPagerAdapter;
35 import android.support.wearable.view.GridViewPager;
36 
37 import java.util.HashMap;
38 import java.util.Map;
39 
40 public class GridExampleActivity extends Activity {
41     private static final int NUM_ROWS = 10;
42     private static final int NUM_COLS = 3;
43 
44     MainAdapter mAdapter;
45     GridViewPager mPager;
46 
47 
48     @Override
onCreate(Bundle savedInstanceState)49     protected void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51         setContentView(R.layout.grid_activity);
52         mPager = (GridViewPager) findViewById(R.id.fragment_container);
53         mAdapter = new MainAdapter(this, getFragmentManager());
54         mPager.setAdapter(mAdapter);
55 
56     }
57 
58     private static class MainAdapter extends FragmentGridPagerAdapter{
59         Map<Point, Drawable> mBackgrounds = new HashMap<Point, Drawable>();
60         private Context mContext;
61 
MainAdapter(Context ctx, FragmentManager fm)62         public MainAdapter(Context ctx, FragmentManager fm) {
63             super(fm);
64             mContext = ctx;
65         }
66 
67         @Override
getRowCount()68         public int getRowCount() {
69             return NUM_ROWS;
70         }
71 
72         @Override
getColumnCount(int rowNum)73         public int getColumnCount(int rowNum) {
74             return NUM_COLS;
75         }
76 
77         @Override
getFragment(int rowNum, int colNum)78         public Fragment getFragment(int rowNum, int colNum) {
79             return MainFragment.newInstance(rowNum, colNum);
80         }
81 
82         @Override
getBackgroundForPage(int row, int column)83         public Drawable getBackgroundForPage(int row, int column) {
84             Point pt = new Point(column, row);
85             Drawable drawable = mBackgrounds.get(pt);
86             if (drawable == null) {
87                 Bitmap bm = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
88                 Canvas c = new Canvas(bm);
89                 Paint p = new Paint();
90                 // Clear previous image.
91                 c.drawRect(0, 0, 200, 200, p);
92                 p.setAntiAlias(true);
93                 p.setTypeface(Typeface.DEFAULT);
94                 p.setTextSize(64);
95                 p.setColor(Color.LTGRAY);
96                 p.setTextAlign(Align.CENTER);
97                 c.drawText(column+ "-" + row, 100, 100, p);
98                 drawable = new BitmapDrawable(mContext.getResources(), bm);
99                 mBackgrounds.put(pt, drawable);
100             }
101             return drawable;
102         }
103     }
104 
105     public static class MainFragment extends CardFragment {
newInstance(int rowNum, int colNum)106         private static MainFragment newInstance(int rowNum, int colNum) {
107             Bundle args = new Bundle();
108             args.putString(CardFragment.KEY_TITLE, "Row:" + rowNum);
109             args.putString(CardFragment.KEY_TEXT, "Col:" + colNum);
110             MainFragment f = new MainFragment();
111             f.setArguments(args);
112             return f;
113         }
114     }
115 }
116