Java tutorial
/* * Copyright (C) 2013 Dario Scoppelletti, <http://www.scoppelletti.it/>. * * 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 it.scoppelletti.mobilepower.app; import java.lang.reflect.*; import android.app.*; import android.content.*; import android.os.*; import android.support.v4.app.*; import org.apache.commons.lang3.*; import org.slf4j.*; import it.scoppelletti.mobilepower.os.*; import it.scoppelletti.mobilepower.reflect.*; /** * Classe di base di un frammento che rappresenta un dialogo. * * @since 1.0 */ public abstract class AbstractDialogFragment extends DialogFragment implements MemberCache.Resolver { private static final int CTOR_ALERTDIALOGBUILDER = 1; private static final Logger myLogger = LoggerFactory.getLogger("AbstractDialog"); /** * Costruttore. */ protected AbstractDialogFragment() { } /** * Gestisce il completamento della creazione dell’attività. * * @param savedInstanceState Stato dell’istanza del frammento. */ @Override public void onActivityCreated(Bundle savedInstanceState) { String tag; Activity activity; ActivitySupport support; super.onActivityCreated(savedInstanceState); activity = getActivity(); if (!(activity instanceof ActivitySupport)) { myLogger.debug("Activity not implement interface ActivitySupport."); return; } tag = getTag(); if (StringUtils.isBlank(tag)) { myLogger.debug("Tag is not set."); return; } support = (ActivitySupport) activity; support.onDialogFragmentShow(tag); } /** * Gestisce la distruzione del dialogo. */ @Override public void onDestroy() { String tag; Activity activity; ActivitySupport support; super.onDestroy(); activity = getActivity(); if (!(activity instanceof ActivitySupport)) { myLogger.debug("Activity not implement interface ActivitySupport."); return; } tag = getTag(); if (StringUtils.isBlank(tag)) { myLogger.debug("Tag is not set."); return; } support = (ActivitySupport) activity; support.onDialogFragmentDismiss(tag); } /** * Istanzia un costruttore di dialoghi. * * @return Oggetto. */ @SuppressWarnings("unchecked") protected final AlertDialog.Builder newAlertDialogBuilder() { Constructor<AlertDialog.Builder> ctor; if (Build.VERSION.SDK_INT < BuildCompat.VERSION_CODES.HONEYCOMB) { return new AlertDialog.Builder(getActivity()); } try { ctor = (Constructor<AlertDialog.Builder>) MemberCache.getInstance().getMember(AlertDialog.Builder.class, AbstractDialogFragment.CTOR_ALERTDIALOGBUILDER, this); return ctor.newInstance(getActivity(), AppUtils.getDialogTheme()); } catch (Exception ex) { myLogger.error("Failed to instantiate AlertDialog.Builder.", ex); return new AlertDialog.Builder(getActivity()); } } public Member resolveMember(Class<?> clazz, int memberCode) { Member member; try { switch (memberCode) { case AbstractDialogFragment.CTOR_ALERTDIALOGBUILDER: member = clazz.getConstructor(Context.class, Integer.TYPE); break; default: throw new NoSuchMethodException(String.format("Unexpected memberCode %1$d.", memberCode)); } } catch (Exception ex) { throw new UnsupportedOperationException(ex.getMessage(), ex); } return member; } }