如何在不引用我的 activity/context 的情况下在 ViewModel 中获取 CameraManager?

How to get CameraManager in ViewModel without holding reference to my activity/context?

在我的应用程序中,我使用了 Android Jetpack 以及导航架构组件。所以现在我想用多个设置片段添加我的设置片段。到目前为止,导航工作正常并且没有问题 - 现在我得到了一个相机设置片段,用户可以在其中对相机进行一些设置。问题是我现在想用设备相机提供的可能分辨率填充 ListPreference。所以我所有的设置片段都应该使用我的 SettingsViewModel 来保存数据。我想我可以在我的 SettingsViewModel 中创建一个方法来获得这些分辨率,但我不知道如何在不违反对我的 activity.

的引用的情况下获得 CameraManager

MainActivity.java

    public class MainActivity extends AppCompatActivity implements
        NavigationView.OnNavigationItemSelectedListener,
        ActivityCompat.OnRequestPermissionsResultCallback,
        PreferenceFragmentCompat.OnPreferenceStartFragmentCallback{

...

    @Override
    public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {

        switch (pref.getTitle().toString()){
            case "Camera":
                Log.i(TAG, "camera settings selected");

                navController.navigate(R.id.cameraSettingFragment);

                }
        return true;
    }

CameraSettingFragment.java

    public class CameraSettingFragment extends PreferenceFragmentCompat {

    private static final String TAG = "CameraSettingFragment";

    private SettingsViewModel mViewModel;

    @Override
    public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
        setPreferencesFromResource(R.xml.camera_preferences, rootKey);

        mViewModel = ViewModelProviders.of(this).get(SettingsViewModel.class);

        setupCameraPreferences();

    }

    private void setupCameraPreferences() {

        getPossibleFrameRateRanges();

        getPossibleCameraResolutions();

    }



    private void getPossibleFrameRateRanges(){

        final ListPreference listPreference = findPreference("framerate");

        CharSequence[] entries = listPreference.getEntries();

        if(entries == null){

            mViewModel.getPossibleFrameRateRanges();

        }

还有我的 ViewModel,我想在其中做一些关于相机特性的事情

    import static android.content.Context.CAMERA_SERVICE;

    public class SettingsViewModel extends AndroidViewModel {

    public SettingsViewModel(@NonNull Application application) {
        super(application);
    }

    public void doAction() {
        // depending on the action, do necessary business logic calls
    }

    public void getPossibleFrameRateRanges() {
        // THIS LINE IS BUGGY AND NEEDS A FIX
        CameraManager manager = (CameraManager) getSystemService(CAMERA_SERVICE);
    }
}

那么我怎样才能不违反这些规定:

Caution: A ViewModel must never reference a view, Lifecycle, or any class that may hold a reference to the activity context.

还是我遗漏了什么?提前致谢!

抱歉耽误了您的时间!我还没有读够......我可以像我那样用 AndroidViewModel 扩展我的 Settings ViewModel class 并使用 application

    public class SettingsViewModel extends AndroidViewModel {

    private Application application;

    public SettingsViewModel(@NonNull Application application) {
        super(application);
        this.application = application;
    }

    public void doAction() {
        // depending on the action, do necessary business logic calls
    }

    public void getPossibleFrameRateRanges() {

        CameraManager manager = (CameraManager) application.getSystemService(CAMERA_SERVICE);
    }
}

非常抱歉,谢谢!