1905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev/*
2905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev * Copyright (C) 2017 The Android Open Source Project
3905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev *
4905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev * Licensed under the Apache License, Version 2.0 (the "License");
5905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev * you may not use this file except in compliance with the License.
6905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev * You may obtain a copy of the License at
7905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev *
8905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev *      http://www.apache.org/licenses/LICENSE-2.0
9905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev *
10905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev * Unless required by applicable law or agreed to in writing, software
11905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev * distributed under the License is distributed on an "AS IS" BASIS,
12905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev * See the License for the specific language governing permissions and
14905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev * limitations under the License.
15905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev */
16905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevpackage android.car.cluster.sample;
17905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
18905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport static android.car.cluster.sample.SampleClusterServiceImpl.LOCAL_BINDING_ACTION;
19905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
20905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.car.cluster.sample.SampleClusterServiceImpl.Listener;
21905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.content.ComponentName;
22905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.content.Intent;
23905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.content.ServiceConnection;
24905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.os.Bundle;
25905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.os.Handler;
26905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.os.IBinder;
27905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.support.v4.app.Fragment;
28905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.support.v4.app.FragmentActivity;
29905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.support.v4.app.FragmentManager;
30905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.support.v4.app.FragmentPagerAdapter;
31905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.support.v4.view.ViewPager;
32905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.text.TextUtils;
33905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.util.Log;
34905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.util.SparseArray;
35905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.view.KeyEvent;
36905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.view.View;
37905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.widget.Button;
38905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport android.widget.TextView;
39905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
40905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport java.lang.reflect.InvocationTargetException;
41905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevimport java.util.HashMap;
42905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
43905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsevpublic class MainClusterActivity extends FragmentActivity
44905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        implements Listener {
45905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    private static final String TAG = MainClusterActivity.class.getSimpleName();
46905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
47905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    private Button mNavButton;
48905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    private Button mPhoneButton;
49905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    private Button mCarInfoButton;
50905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    private Button mMusicButton;
51905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    private TextView mTextOverlay;
52905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    private ViewPager mPager;
53905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
54905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    private SampleClusterServiceImpl mService;
55905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
56905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    private final Handler mHandler = new Handler();
57905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
58905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    private HashMap<Button, Facet<?>> mButtonToFacet = new HashMap<>();
59905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    private SparseArray<Facet<?>> mOrderToFacet = new SparseArray<>();
60905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
61905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    private final View.OnFocusChangeListener mFacetButtonFocusListener =
62905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            new View.OnFocusChangeListener() {
63905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        @Override
64905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        public void onFocusChange(View v, boolean hasFocus) {
65905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            if (hasFocus) {
66905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                mPager.setCurrentItem(mButtonToFacet.get(v).order);
67905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            }
68905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        }
69905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    };
70905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
71905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    @Override
72905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    protected void onCreate(Bundle savedInstanceState) {
73905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        super.onCreate(savedInstanceState);
74905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        setContentView(R.layout.activity_main);
75905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
76905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        Intent intent = new Intent(this, SampleClusterServiceImpl.class);
77905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        intent.setAction(LOCAL_BINDING_ACTION);
78905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        bindService(intent,
79905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                new ServiceConnection() {
80905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                    @Override
81905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                    public void onServiceConnected(ComponentName name, IBinder service) {
82905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                        Log.i(TAG, "onServiceConnected, name: " + name + ", service: " + service);
83905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                        mService = ((SampleClusterServiceImpl.LocalBinder) service)
84905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                                .getService();
85905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                        mService.registerListener(MainClusterActivity.this);
86905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                    }
87905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
88905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                    @Override
89905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                    public void onServiceDisconnected(ComponentName name) {
90905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                        Log.i(TAG, "onServiceDisconnected, name: " + name);
91905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                        mService = null;
92905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                    }
93905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                }, BIND_AUTO_CREATE);
94905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
95905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        mNavButton = findViewById(R.id.btn_nav);
96905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        mPhoneButton = findViewById(R.id.btn_phone);
97905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        mCarInfoButton = findViewById(R.id.btn_car_info);
98905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        mMusicButton = findViewById(R.id.btn_music);
99905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        mTextOverlay = findViewById(R.id.text_overlay);
100905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
101905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        registerFacets(
102905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                new Facet<>(mNavButton, 0, NavigationFragment.class),
103905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                new Facet<>(mPhoneButton, 1, PhoneFragment.class),
104905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                new Facet<>(mMusicButton, 2, MusicFragment.class),
105905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                new Facet<>(mCarInfoButton, 3, CarInfoFragment.class));
106905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
107905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        mPager = (ViewPager) findViewById(R.id.pager);
108905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        mPager.setAdapter(new ClusterPageAdapter(getSupportFragmentManager()));
109905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
110905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        mNavButton.requestFocus();
111905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    }
112905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
113905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    @Override
114905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    protected void onDestroy() {
115905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        super.onDestroy();
116905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        if (mService != null) {
117905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            mService.unregisterListener();
118905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        }
119905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    }
120905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
121905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    @Override
122905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    public void onShowToast(String text) {
123905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        if (mTextOverlay.getVisibility() == View.VISIBLE) {
124905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            if (!TextUtils.isEmpty(mTextOverlay.getText())) {
125905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                mTextOverlay.setText(mTextOverlay.getText() + "\n" + text);
126905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            } else {
127905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                mTextOverlay.setText(text);
128905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            }
129905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        }
130905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
131905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        mTextOverlay.setVisibility(View.VISIBLE);
132905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
133905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        mHandler.removeCallbacksAndMessages(null);
134905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        mHandler.postDelayed(() -> {
135905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            mTextOverlay.setVisibility(View.GONE);
136905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            mTextOverlay.setText("");
137905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        }, 3000);
138905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    }
139905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
140905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    @Override
141905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    public void onKeyEvent(KeyEvent event) {
142905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        Log.i(TAG, "onKeyEvent, event: " + event);
143905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        dispatchKeyEvent(event);  // TODO: dispatch event doesn't work for some reason.
144905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
145905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        if (event.getAction() == KeyEvent.ACTION_DOWN) {
146905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
147905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                int nextItem = (mPager.getCurrentItem() + 1) % mButtonToFacet.size();
148905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                mOrderToFacet.get(nextItem).button.requestFocus();
149905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
150905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                int nextItem = (mPager.getCurrentItem() - 1);
151905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                if (nextItem < 0) nextItem =  mButtonToFacet.size() - 1;
152905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                mOrderToFacet.get(nextItem).button.requestFocus();
153905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            }
154905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        }
155905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    }
156905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
157905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    @Override
158905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    public boolean dispatchKeyEvent(KeyEvent event) {
159905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        boolean consumed = super.dispatchKeyEvent(event);
160905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        Log.i(TAG, "dispatchKeyEvent, event: " + event + ", consumed: " + consumed);
161905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        return consumed;
162905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    }
163905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
164905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    public class ClusterPageAdapter extends FragmentPagerAdapter {
165905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        public ClusterPageAdapter(FragmentManager fm) {
166905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            super(fm);
167905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        }
168905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
169905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        @Override
170905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        public int getCount() {
171905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            return mButtonToFacet.size();
172905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        }
173905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
174905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        @Override
175905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        public Fragment getItem(int position) {
176905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            return mOrderToFacet.get(position).getOrCreateFragment();
177905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        }
178905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    }
179905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
180905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    private void registerFacets(Facet<?>... facets) {
181905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        for (Facet<?> f : facets) {
182905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            registerFacet(f);
183905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        }
184905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    }
185905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
186905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    private <T> void registerFacet(Facet<T> facet) {
187905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        mOrderToFacet.append(facet.order, facet);
188905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        mButtonToFacet.put(facet.button, facet);
189905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
190905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        facet.button.setOnFocusChangeListener(mFacetButtonFocusListener);
191905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    }
192905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
193905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    private static class Facet<T> {
194905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        Button button;
195905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        Class<T> clazz;
196905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        int order;
197905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
198905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        Facet(Button button, int order, Class<T> clazz) {
199905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            this.button = button;
200905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            this.order = order;
201905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            this.clazz = clazz;
202905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        }
203905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
204905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        private Fragment mFragment;
205905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
206905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        Fragment getOrCreateFragment() {
207905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            if (mFragment == null) {
208905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                try {
209905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                    mFragment = (Fragment) clazz.getConstructors()[0].newInstance();
210905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
211905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                    throw new RuntimeException(e);
212905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev                }
213905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            }
214905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev            return mFragment;
215905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        }
216905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    }
217905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev
218905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    SampleClusterServiceImpl getService() {
219905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev        return mService;
220905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev    }
221905968cf95d4c8608d6d9351b5dd10fe994a1220Pavel Maltsev}
222