Search in sources :

Example 1 with SettingItem

use of com.ichi2.ui.SettingItem in project AnkiChinaAndroid by ankichinateam.

the class SettingFragment method onCreateView.

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Timber.i("on create view in setting fragment");
    if (mRoot == null) {
        final SharedPreferences preferences = getPreferences();
        mRoot = inflater.inflate(R.layout.fragment_setting, container, false);
        rl_login = mRoot.findViewById(R.id.rl_login);
        mLl_defaultLink = mRoot.findViewById(R.id.ll_default_link);
        mRl_user_name = mRoot.findViewById(R.id.tv_user_name);
        mTv_logout = mRoot.findViewById(R.id.tv_quit);
        mIv_entrance = mRoot.findViewById(R.id.iv_entrance);
        mRl_cloud_space = mRoot.findViewById(R.id.tv_cloud_space);
        mLl_switch_server = mRoot.findViewById(R.id.ll_switch_server);
        mTv_server_name = mRoot.findViewById(R.id.server_name);
        mTv_switch_server = mRoot.findViewById(R.id.tv_switch_server);
        rl_login.setOnClickListener(v -> ((DeckPicker) getAnkiActivity()).loginToSyncServer());
        mRoot.findViewById(R.id.rl_personal_setting).setOnClickListener(v -> {
            Timber.i("Navigating to settings");
            mOldColPath = CollectionHelper.getCurrentAnkiDroidDirectory(getAnkiActivity());
            // Remember the theme we started with so we can restart the Activity if it changes
            mOldTheme = Themes.getCurrentTheme(getContext());
            getAnkiActivity().startActivityForResultWithAnimation(new Intent(getAnkiActivity(), Preferences.class), REQUEST_PREFERENCES_UPDATE, ActivityTransitionAnimation.FADE);
        });
        // mRoot.findViewById(R.id.rl_sync_set).setOnClickListener(v -> {
        // Timber.i("AnkiDroid directory inaccessible");
        // Intent i = Preferences.getPreferenceSubscreenIntent(getAnkiActivity(), "com.ichi2.anki.prefs.general");
        // getAnkiActivity().startActivityWithAnimation(i, ActivityTransitionAnimation.FADE);
        // });
        mNightModeSwitch = mRoot.findViewById(R.id.switch_dark_mode);
        mNightModeSwitch.setChecked(preferences.getBoolean(NIGHT_MODE_PREFERENCE, false));
        mNightModeSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            applyNightMode(isChecked);
        });
        mAutoSyncSwitch = mRoot.findViewById(R.id.switch_auto_sync);
        mAutoSyncSwitch.setChecked(preferences.getBoolean("automaticSyncMode", true));
        mAutoSyncSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            preferences.edit().putBoolean("automaticSyncMode", isChecked).apply();
        });
        mRoot.findViewById(R.id.rl_dark_mode).setOnClickListener(v -> {
            Timber.i("Toggling Night Mode");
            mNightModeSwitch.performClick();
        });
        mRoot.findViewById(R.id.vip_power).setOnClickListener(this);
        mRoot.findViewById(R.id.rl_anki_course).setOnClickListener(this);
        mRoot.findViewById(R.id.rl_team).setOnClickListener(this);
        mRoot.findViewById(R.id.rl_version).setOnClickListener(this);
        mRoot.findViewById(R.id.rl_feedback).setOnClickListener(this);
        mRoot.findViewById(R.id.user_protocol).setOnClickListener(this);
        mRoot.findViewById(R.id.user_private).setOnClickListener(this);
        mRoot.findViewById(R.id.rl_market_like).setOnClickListener(this);
        mLl_switch_server.setOnClickListener(this);
        mVipText = mRoot.findViewById(R.id.vip_text);
        mVipPower = mRoot.findViewById(R.id.vip_power);
        mVipText.setText(mVip ? getVipString(mVipDay) : getNotVipString());
        mVipPower.setText(mVip ? "查看权益" : "立即开通");
        OKHttpUtil.get(Consts.ANKI_CHINA_BASE + Consts.API_VERSION + "configs/1", "", "", new OKHttpUtil.MyCallBack() {

            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, String token, Object arg1, Response response) throws IOException {
                if (response.isSuccessful()) {
                    Timber.i("initMoreDrawerMenuItem successfully!:%s", response.body());
                    try {
                        final JSONObject object = new JSONObject(response.body().string());
                        final JSONArray items = object.getJSONArray("data");
                        Timber.i("initMoreDrawerMenuItem %d ", items.length());
                        List<DynamicItem> dynamicItems = new ArrayList<>();
                        for (int i = 0; i < items.length(); i++) {
                            JSONObject jsonObject = items.getJSONObject(i);
                            String title = jsonObject.optString("title");
                            String image = jsonObject.optString("image_url");
                            String link = jsonObject.optString("target_url");
                            Drawable drawable = null;
                            try {
                                drawable = Drawable.createFromStream(new URL(image).openStream(), title + ".jpg");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            dynamicItems.add(new DynamicItem(title, link, drawable));
                            Timber.i("load drawable result %s,%s ,%s  ", title, image, link);
                        }
                        getAnkiActivity().runOnUiThread(() -> {
                            mLl_defaultLink.removeAllViews();
                            for (int i = 0; i < dynamicItems.size(); i++) {
                                int finalI = i;
                                int[] attrs = new int[] { R.attr.settingItemBackgroundTop, R.attr.settingItemBackground, R.attr.settingItemBackgroundBottom, R.attr.settingItemBackgroundRound };
                                TypedArray ta = getAnkiActivity().obtainStyledAttributes(attrs);
                                Drawable background;
                                background = ta.getDrawable(dynamicItems.size() == 1 ? 3 : finalI == 0 ? 0 : finalI == dynamicItems.size() - 1 ? 2 : 1);
                                DynamicItem dynamicItem = dynamicItems.get(finalI);
                                SettingItem item = new SettingItem(getContext(), dynamicItem.title, dynamicItem.drawable);
                                item.findViewById(R.id.rl_root).setBackground(background);
                                item.setOnClickListener(v -> {
                                    WebViewActivity.openUrlInApp(getAnkiActivity(), dynamicItem.link, "", -1);
                                });
                                mLl_defaultLink.addView(item);
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    Timber.e("initMoreDrawerMenuItem failed, error code %d", response.code());
                }
            }
        });
    }
    return mRoot;
}
Also used : StringFormat(org.acra.data.StringFormat) Text(org.w3c.dom.Text) LinearLayout(android.widget.LinearLayout) Bundle(android.os.Bundle) URL(java.net.URL) NonNull(androidx.annotation.NonNull) WindowManager(android.view.WindowManager) ImageView(android.widget.ImageView) ForeGroundColorSpan(com.ichi2.anki.DeckPicker.ForeGroundColorSpan) Drawable(android.graphics.drawable.Drawable) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) Handler(android.os.Handler) View(android.view.View) ForegroundColorSpan(android.text.style.ForegroundColorSpan) CHANGE_ACCOUNT(com.ichi2.anki.DeckPicker.CHANGE_ACCOUNT) URL_ANKI_COURSE(com.ichi2.libanki.Consts.URL_ANKI_COURSE) ViewGroup(android.view.ViewGroup) Timber(timber.log.Timber) List(java.util.List) TextView(android.widget.TextView) Nullable(androidx.annotation.Nullable) HostNumFactory(com.ichi2.anki.web.HostNumFactory) Consts(com.ichi2.libanki.Consts) OKHttpUtil(com.ichi2.utils.OKHttpUtil) MobclickAgent(com.umeng.analytics.MobclickAgent) RelativeLayout(android.widget.RelativeLayout) Window(android.view.Window) DeckPicker.goAppShop(com.ichi2.anki.DeckPicker.goAppShop) URL_FEEDBACK(com.ichi2.libanki.Consts.URL_FEEDBACK) Spanned(android.text.Spanned) Dialog(android.app.Dialog) Intent(android.content.Intent) Collection(com.ichi2.libanki.Collection) URL_PRIVATE(com.ichi2.libanki.Consts.URL_PRIVATE) TypedArray(android.content.res.TypedArray) ArrayList(java.util.ArrayList) SpannableStringBuilder(android.text.SpannableStringBuilder) SyncErrorDialog(com.ichi2.anki.dialogs.SyncErrorDialog) Toast(android.widget.Toast) Connection(com.ichi2.async.Connection) Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) ActionBar(android.app.ActionBar) URL_VERSION(com.ichi2.libanki.Consts.URL_VERSION) SettingItem(com.ichi2.ui.SettingItem) SwitchCompat(androidx.appcompat.widget.SwitchCompat) SpannableString(android.text.SpannableString) LayoutInflater(android.view.LayoutInflater) URL_VOLUNTEER(com.ichi2.libanki.Consts.URL_VOLUNTEER) URL_USER_PROTOCOL(com.ichi2.libanki.Consts.URL_USER_PROTOCOL) TextUtils(android.text.TextUtils) BackgroundColorSpan(android.text.style.BackgroundColorSpan) IOException(java.io.IOException) Themes(com.ichi2.themes.Themes) Color(android.graphics.Color) Gravity(android.view.Gravity) TaskStackBuilder(androidx.core.app.TaskStackBuilder) SharedPreferences(android.content.SharedPreferences) ActivityTransitionAnimation(com.ichi2.anim.ActivityTransitionAnimation) JSONArray(org.json.JSONArray) Resources(android.content.res.Resources) Call(okhttp3.Call) OKHttpUtil(com.ichi2.utils.OKHttpUtil) SharedPreferences(android.content.SharedPreferences) JSONArray(org.json.JSONArray) Drawable(android.graphics.drawable.Drawable) Intent(android.content.Intent) IOException(java.io.IOException) SpannableString(android.text.SpannableString) URL(java.net.URL) JSONException(org.json.JSONException) IOException(java.io.IOException) Response(okhttp3.Response) JSONObject(org.json.JSONObject) TypedArray(android.content.res.TypedArray) SettingItem(com.ichi2.ui.SettingItem) JSONObject(org.json.JSONObject) List(java.util.List) ArrayList(java.util.ArrayList) SharedPreferences(android.content.SharedPreferences) Nullable(androidx.annotation.Nullable)

Aggregations

ActionBar (android.app.ActionBar)1 Dialog (android.app.Dialog)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 Resources (android.content.res.Resources)1 TypedArray (android.content.res.TypedArray)1 Color (android.graphics.Color)1 Drawable (android.graphics.drawable.Drawable)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 SpannableString (android.text.SpannableString)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 Spanned (android.text.Spanned)1 TextUtils (android.text.TextUtils)1 BackgroundColorSpan (android.text.style.BackgroundColorSpan)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 Gravity (android.view.Gravity)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1