Java Code Examples for android.view.LayoutInflater#cloneInContext()

The following examples show how to use android.view.LayoutInflater#cloneInContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PluginContextWrapper.java    From Neptune with Apache License 2.0 6 votes vote down vote up
@Override
public Object getSystemService(String name) {

    if (LAYOUT_INFLATER_SERVICE.equals(name)) {
        if (mLayoutInflater == null) {
            // 重写插件Application Context的获取LayoutInflater方法,解决插件使用Application Context
            // 无法访问插件资源的问题,原因是LayoutInflater的构造函数使用的是Base Context的outerContext,
            // 而这个OuterContext是宿主的Application
            LayoutInflater inflater = (LayoutInflater) super.getSystemService(name);
            mLayoutInflater = forApp ? inflater.cloneInContext(this) : inflater;
            // 设置mPrivateFactory,修复多个插件同时依赖使用同名View的问题,比如android design库
            LayoutInflaterCompat.setPrivateFactory(inflater);
        }
        return mLayoutInflater;
    }

    return super.getSystemService(name);
}
 
Example 2
Source File: NightOwl.java    From NightOwl with Apache License 2.0 6 votes vote down vote up
public static void owlBeforeCreate(Activity activity){
    Window window = activity.getWindow();
    LayoutInflater layoutInflater = window.getLayoutInflater();

    // replace the inflater in window
    LayoutInflater injectLayoutInflater1 = Factory4InjectedInflater.newInstance(layoutInflater, activity);
    injectLayoutInflater(injectLayoutInflater1
            , activity.getWindow()
            , activity.getWindow().getClass()
            , WINDOW_INFLATER);

    // replace the inflater in current ContextThemeWrapper
    LayoutInflater injectLayoutInflater2 = injectLayoutInflater1.cloneInContext(activity);
    injectLayoutInflater(injectLayoutInflater2
            , activity
            , ContextThemeWrapper.class
            , THEME_INFLATER);

    // insert owlViewContext into root view.
    View v = activity.getWindow().getDecorView();
    OwlViewContext owlObservable = new OwlViewContext();
    insertViewContext(v, owlObservable);

}
 
Example 3
Source File: ShapeThemingDemoFragment.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public View onCreateView(
    LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
  this.wrappedContext = new ContextThemeWrapper(getContext(), getShapeTheme());
  LayoutInflater layoutInflaterWithThemedContext =
      layoutInflater.cloneInContext(wrappedContext);
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getActivity().getWindow();
    statusBarColor = window.getStatusBarColor();
    final TypedValue value = new TypedValue();
    wrappedContext
        .getTheme()
        .resolveAttribute(R.attr.colorPrimaryDark, value, true);
    window.setStatusBarColor(value.data);
  }

  return super.onCreateView(layoutInflaterWithThemedContext, viewGroup, bundle);
}
 
Example 4
Source File: CompactNavigationListFragmentDelegate.java    From material-navigation-drawer with Apache License 2.0 5 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (mTheme > 0) {
        Context newContext = new ContextThemeWrapper(inflater.getContext(), mTheme);
        inflater = inflater.cloneInContext(newContext);
    }
    mInflater = inflater;

    View view = inflater.inflate(R.layout.mnd_list_compact, container, false);
    mListView = (ListView) view.findViewById(R.id.mnd_list_compact);
    return view;
}
 
Example 5
Source File: NavigationListFragmentDelegate.java    From material-navigation-drawer with Apache License 2.0 5 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (mTheme > 0) {
        Context newContext = new ContextThemeWrapper(inflater.getContext(), mTheme);
        inflater = inflater.cloneInContext(newContext);
    }
    mInflater = inflater;

    mView = inflater.inflate(R.layout.mnd_list, container, false);
    mListView = (ListView) mView.findViewById(R.id.mnd_list);
    mPinnedContainer = (ViewGroup) mView.findViewById(R.id.mnd_section_pinned);
    mPinnedDivider = mView.findViewById(R.id.mnd_divider_pinned);
    return mView;
}
 
Example 6
Source File: ZeusHelper.java    From ZeusPlugin with MIT License 5 votes vote down vote up
/**
 * 配置LAYOUT_INFLATER_SERVICE时的一些参数
 *
 * @param context       调用着的context
 * @param systemServcie systemServer对象
 * @param name          server的名字
 * @return systemServer对象
 */
public static Object getSystemService(Context context, Object systemServcie, String name) {
    if (Context.LAYOUT_INFLATER_SERVICE.equals(name)) {
        LayoutInflater inflater = (LayoutInflater) systemServcie;
        inflater.cloneInContext(context);
        //使用某些加固之后该inflater里的mContext变量一直是系统的context,根本不是当前Context
        //所以这里手动设置一次
        PluginUtil.setField(inflater, "mContext", context);
        return inflater;
    }
    return systemServcie;
}
 
Example 7
Source File: FlagSecureHelper.java    From cwac-security with Apache License 2.0 5 votes vote down vote up
public static Object getWrappedSystemService(Object service,
                                             String name,
                                             boolean wrapLayoutInflater) {
  if (Context.WINDOW_SERVICE.equals(name)) {
    boolean goAhead=true;

    for (StackTraceElement entry : Thread.currentThread().getStackTrace()) {
      try {
        Class cls=Class.forName(entry.getClassName());

        if (Dialog.class.isAssignableFrom(cls)) {
          goAhead=false;
          break;
        }
      }
      catch (ClassNotFoundException e) {
        // ???
      }
    }

    if (goAhead) {
      service=new SecureWindowManagerWrapper((WindowManager)service);
    }
  }
  else if (Context.LAYOUT_INFLATER_SERVICE.equals(name) && wrapLayoutInflater) {
    LayoutInflater original=(LayoutInflater)service;
    Context securified=
      new SecureContextWrapper(original.getContext(), true, true);

    service=original.cloneInContext(securified);
  }

  return(service);
}
 
Example 8
Source File: BaseFragment.java    From PocketEOS-Android with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (mView == null) {
        boolean isLight = Utils.getSpUtils().getString("loginmode").equals("phone");
        Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), isLight ? R.style.ThemeLight : R.style.ThemeDark);
        LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
        mView = localInflater.inflate(getContentViewLayoutID(), null);
    }
    ViewGroup parent = (ViewGroup) mView.getParent();
    if (parent != null) {
        parent.removeView(mView);
    }
    return mView;
}
 
Example 9
Source File: PluginInterceptApplication.java    From Phantom with Apache License 2.0 5 votes vote down vote up
private void initLayoutInflater() {
    LayoutInflater layoutInflater = ((LayoutInflater) mContentProxy.getContext().getSystemService(Context
            .LAYOUT_INFLATER_SERVICE));
    if (null != layoutInflater) {
        mLayoutInflater = layoutInflater.cloneInContext(this);
    }
}
 
Example 10
Source File: BaseFragment.java    From timecat with Apache License 2.0 5 votes vote down vote up
@SuppressLint("RestrictedApi")
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    context = getContext();
    activity = getActivity();
    if (fragmentLayout() != 0) {
        final Context contextThemeWrapper = new ContextThemeWrapper(getContext(), getContext().getTheme());
        LayoutInflater themeAwareInflater = inflater.cloneInContext(contextThemeWrapper);
        View view = themeAwareInflater.inflate(fragmentLayout(), container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }
    return super.onCreateView(inflater, container, savedInstanceState);
}
 
Example 11
Source File: ActivityWrapper.java    From Neptune with Apache License 2.0 5 votes vote down vote up
@Override
public Object getSystemService(@NonNull String name) {
    if (LAYOUT_INFLATER_SERVICE.equals(name)) {
        LayoutInflater layoutInflater = (LayoutInflater) mPluginContext.getSystemService(name);
        return layoutInflater.cloneInContext(this);
    }
    return mOriginActivity.getSystemService(name);
}
 
Example 12
Source File: FetchPreferencesActivity.java    From mage-android with Apache License 2.0 4 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.AppTheme);
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
    return super.onCreateView(localInflater, container, savedInstanceState);
}
 
Example 13
Source File: TimeFragment.java    From SlideDayTimePicker with Apache License 2.0 4 votes vote down vote up
/**
 * Create and return the user interface view for this fragment.
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    int theme = getArguments().getInt("theme");
    int initialHour = getArguments().getInt("hour");
    int initialMinute = getArguments().getInt("minute");
    boolean isClientSpecified24HourTime = getArguments().getBoolean("isClientSpecified24HourTime");
    boolean is24HourTime = getArguments().getBoolean("is24HourTime");

    // Unless we inflate using a cloned inflater with a Holo theme,
    // on Lollipop devices the TimePicker will be the new-style
    // radial TimePicker, which is not what we want. So we will
    // clone the inflater that we're given but with our specified
    // theme, then inflate the layout with this new inflater.

    Context contextThemeWrapper = new ContextThemeWrapper(
            getActivity(),
            theme == SlideDayTimePicker.HOLO_DARK ?
                    android.R.style.Theme_Holo : android.R.style.Theme_Holo_Light);
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
    View v = localInflater.inflate(R.layout.fragment_time, container, false);

    mTimePicker = (TimePicker) v.findViewById(R.id.timePicker);
    // block keyboard popping up on touch
    mTimePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
    mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
        {
            mCallback.onTimeChanged(hourOfDay, minute);
        }
    });

    // If the client specifies a 24-hour time format, set it on
    // the TimePicker.
    if (isClientSpecified24HourTime)
    {
        mTimePicker.setIs24HourView(is24HourTime);
    }
    else
    {
        // If the client does not specify a 24-hour time format, use the
        // device default.
        mTimePicker.setIs24HourView(DateFormat.is24HourFormat(
            getTargetFragment().getActivity()));
    }

    mTimePicker.setCurrentHour(initialHour);
    mTimePicker.setCurrentMinute(initialMinute);

    // Fix for the bug where a TimePicker's onTimeChanged() is not called when
    // the user toggles the AM/PM button. Only applies to 4.0.0 and 4.0.3.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH &&
        Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
    {
        fixTimePickerBug18982();
    }

    return v;
}
 
Example 14
Source File: ThemeUtil.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
public static LayoutInflater getThemedInflater(@NonNull Context context, @NonNull LayoutInflater inflater, @StyleRes int theme) {
  Context contextThemeWrapper = new ContextThemeWrapper(context, theme);
  return inflater.cloneInContext(contextThemeWrapper);
}
 
Example 15
Source File: DayFragment.java    From SlideDayTimePicker with Apache License 2.0 4 votes vote down vote up
/**
 * Create and return the user interface view for this fragment.
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    int initialDay = getArguments().getInt("initialDay");
    boolean isCustomDaysArraySpecified = getArguments().getBoolean("isCustomDaysArraySpecified");

    String[] daysArray;
    if (isCustomDaysArraySpecified)
    {
        daysArray = getArguments().getStringArray("customDaysArray");
    }
    else
    {
        daysArray = getResources().getStringArray(R.array.days_array);
    }

    // Unless we inflate using a cloned inflater with a Holo theme,
    // on Lollipop devices the TimePicker will be the new-style
    // radial TimePicker, which is not what we want. So we will
    // clone the inflater that we're given but with our specified
    // theme, then inflate the layout with this new inflater.
    int theme = getArguments().getInt("theme");

    Context contextThemeWrapper = new ContextThemeWrapper(
            getActivity(),
            theme == SlideDayTimePicker.HOLO_DARK ?
                    android.R.style.Theme_Holo : android.R.style.Theme_Holo_Light);

    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

    View v = localInflater.inflate(R.layout.fragment_day, container, false);

    CustomNumberPicker dayPicker = (CustomNumberPicker) v.findViewById(R.id.dayPicker);
    // remove blinking cursor from NumberPicker
    enableNumberPickerEditing(dayPicker, false);
    // block keyboard popping up on touch
    dayPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
    dayPicker.setMinValue(0);
    dayPicker.setMaxValue(daysArray.length - 1);
    dayPicker.setDisplayedValues(daysArray);
    dayPicker.setValue(initialDay);
    dayPicker.setOnValueChangedListener(new OnValueChangeListener() {

        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal)
        {
            mCallback.onDayChanged(newVal);
        }
    });

    return v;
}
 
Example 16
Source File: PreferencesActivity.java    From geopackage-mapcache-android with MIT License 4 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.AppTheme);
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
    return super.onCreateView(localInflater, container, savedInstanceState);
}
 
Example 17
Source File: PreferenceFragment.java    From MaterialPreference with Apache License 2.0 4 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    TypedArray a = mStyledContext.obtainStyledAttributes(null,
            R.styleable.PreferenceFragment,
            R.attr.preferenceFragmentStyle,
            0);

    mLayoutResId = a.getResourceId(R.styleable.PreferenceFragment_android_layout,
            mLayoutResId);

    final Drawable divider = a.getDrawable(
            R.styleable.PreferenceFragment_android_divider);
    final int dividerHeight = a.getDimensionPixelSize(
            R.styleable.PreferenceFragment_android_dividerHeight, -1);
    final boolean allowDividerAfterLastItem = a.getBoolean(
            R.styleable.PreferenceFragment_allowDividerAfterLastItem, false);

    a.recycle();

    // Need to theme the inflater to pick up the preferenceFragmentListStyle
    final TypedValue tv = new TypedValue();
    getActivity().getTheme().resolveAttribute(R.attr.preferenceTheme, tv, true);
    final int theme = tv.resourceId;

    final Context themedContext = new ContextThemeWrapper(inflater.getContext(), theme);
    final LayoutInflater themedInflater = inflater.cloneInContext(themedContext);

    final View view = themedInflater.inflate(mLayoutResId, container, false);

    final View rawListContainer = view.findViewById(R.id.list_container);
    if (!(rawListContainer instanceof ViewGroup)) {
        throw new RuntimeException("Content has view with id attribute "
                + "'R.id.list_container' that is not a ViewGroup class");
    }

    final ViewGroup listContainer = (ViewGroup) rawListContainer;

    final RecyclerView listView = onCreateRecyclerView(themedInflater, listContainer,
            savedInstanceState);
    if (listView == null) {
        throw new RuntimeException("Could not create RecyclerView");
    }

    mList = listView;

    mDividerDecoration = onCreateItemDecoration();
    if (mDividerDecoration != null) {
        mList.addItemDecoration(mDividerDecoration);
    }

    setDivider(divider);
    if (dividerHeight != -1) {
        setDividerHeight(dividerHeight);
    }
    mDividerDecoration.setAllowDividerAfterLastItem(allowDividerAfterLastItem);

    listContainer.addView(mList);
    mHandler.post(mRequestFocus);

    return view;
}
 
Example 18
Source File: SecureSlide.java    From Puff-Android with MIT License 4 votes vote down vote up
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(this.getActivity(), this.getArguments().getInt("com.heinrichreimersoftware.materialintro.SimpleFragment.ARGUMENT_THEME_RES"));
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
    return localInflater.inflate(this.getArguments().getInt("com.heinrichreimersoftware.materialintro.SimpleFragment.ARGUMENT_LAYOUT_RES"), container, false);
}
 
Example 19
Source File: DateFragment.java    From SlideDateTimePicker with Apache License 2.0 4 votes vote down vote up
/**
 * Create and return the user interface view for this fragment.
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    int theme = getArguments().getInt("theme");
    int initialYear = getArguments().getInt("year");
    int initialMonth = getArguments().getInt("month");
    int initialDay = getArguments().getInt("day");
    Date minDate = (Date) getArguments().getSerializable("minDate");
    Date maxDate = (Date) getArguments().getSerializable("maxDate");

    // Unless we inflate using a cloned inflater with a Holo theme,
    // on Lollipop devices the DatePicker will be the new-style
    // DatePicker, which is not what we want. So we will
    // clone the inflater that we're given but with our specified
    // theme, then inflate the layout with this new inflater.

    Context contextThemeWrapper = new ContextThemeWrapper(
            getActivity(),
            theme == SlideDateTimePicker.HOLO_DARK ?
                     android.R.style.Theme_Holo :
                     android.R.style.Theme_Holo_Light);

    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

    View v = localInflater.inflate(R.layout.fragment_date, container, false);

    mDatePicker = (CustomDatePicker) v.findViewById(R.id.datePicker);
    // block keyboard popping up on touch
    mDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
    mDatePicker.init(
        initialYear,
        initialMonth,
        initialDay,
        new OnDateChangedListener() {

            @Override
            public void onDateChanged(DatePicker view, int year,
                    int monthOfYear, int dayOfMonth)
            {
                mCallback.onDateChanged(year, monthOfYear, dayOfMonth);
            }
        });

    if (minDate != null)
        mDatePicker.setMinDate(minDate.getTime());

    if (maxDate != null)
        mDatePicker.setMaxDate(maxDate.getTime());

    return v;
}
 
Example 20
Source File: AppWidgetHostView.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Inflate and return the default layout requested by AppWidget provider.
 */
protected View getDefaultView() {
    if (LOGD) {
        Log.d(TAG, "getDefaultView");
    }
    View defaultView = null;
    Exception exception = null;

    try {
        if (mInfo != null) {
            Context theirContext = getRemoteContext();
            mRemoteContext = theirContext;
            LayoutInflater inflater = (LayoutInflater)
                    theirContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater = inflater.cloneInContext(theirContext);
            inflater.setFilter(INFLATER_FILTER);
            AppWidgetManager manager = AppWidgetManager.getInstance(mContext);
            Bundle options = manager.getAppWidgetOptions(mAppWidgetId);

            int layoutId = mInfo.initialLayout;
            if (options.containsKey(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY)) {
                int category = options.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY);
                if (category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD) {
                    int kgLayoutId = mInfo.initialKeyguardLayout;
                    // If a default keyguard layout is not specified, use the standard
                    // default layout.
                    layoutId = kgLayoutId == 0 ? layoutId : kgLayoutId;
                }
            }
            defaultView = inflater.inflate(layoutId, this, false);
        } else {
            Log.w(TAG, "can't inflate defaultView because mInfo is missing");
        }
    } catch (RuntimeException e) {
        exception = e;
    }

    if (exception != null) {
        Log.w(TAG, "Error inflating AppWidget " + mInfo + ": " + exception.toString());
    }

    if (defaultView == null) {
        if (LOGD) Log.d(TAG, "getDefaultView couldn't find any view, so inflating error");
        defaultView = getErrorView();
    }

    return defaultView;
}