/* * Copyright 2013 str4d * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.i2p.android.wizard.ui; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView; import net.i2p.android.router.R; import net.i2p.android.wizard.model.Page; import net.i2p.android.wizard.model.SingleFixedBooleanPage; public class SingleBooleanFragment extends Fragment { private static final String ARG_KEY = "key"; private PageFragmentCallbacks mCallbacks; private SingleFixedBooleanPage mPage; private CheckBox mCheckBox; public static SingleBooleanFragment create(String key) { Bundle args = new Bundle(); args.putString(ARG_KEY, key); SingleBooleanFragment fragment = new SingleBooleanFragment(); fragment.setArguments(args); return fragment; } public SingleBooleanFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle args = getArguments(); String mKey = args.getString(ARG_KEY); mPage = (SingleFixedBooleanPage) mCallbacks.onGetPage(mKey); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_wizard_page_single_boolean, container, false); ((TextView) rootView.findViewById(android.R.id.title)).setText(mPage.getTitle()); ((TextView) rootView.findViewById(R.id.wizard_text_field_desc)).setText(mPage.getDesc()); mCheckBox = ((CheckBox) rootView.findViewById(R.id.wizard_check_box)); mCheckBox.setChecked(mPage.getData().getBoolean(Page.SIMPLE_DATA_KEY)); if (mPage.getLabel() != null) mCheckBox.setText(mPage.getLabel()); return rootView; } @Override public void onAttach(Activity activity) { super.onAttach(activity); if (!(activity instanceof PageFragmentCallbacks)) { throw new ClassCastException("Activity must implement PageFragmentCallbacks"); } mCallbacks = (PageFragmentCallbacks) activity; } @Override public void onDetach() { super.onDetach(); mCallbacks = null; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mPage.getData().putBoolean(Page.SIMPLE_DATA_KEY, isChecked); mPage.notifyDataChanged(); } }); } }