public static Message.StartUpType StartUpTypeToMessage(AppxStartUpType value)
        {
            switch (value)
            {
            case AppxStartUpType.Foreground:
                return(Message.StartUpType.Foreground);

            case AppxStartUpType.Background:
                return(Message.StartUpType.Background);
            }
            return(Message.StartUpType.None);
        }
        public static string StartUpTypeToString(AppxStartUpType startUpType)
        {
            switch (startUpType)
            {
            case AppxStartUpType.Foreground:
                return(JsonStartUpForeground);

            case AppxStartUpType.Background:
                return(JsonStartUpBackground);
            }
            return(JsonStartUpNone);
        }
        public static AppxStartUpType StartUpTypeFromString(string value)
        {
            AppxStartUpType startUp = AppxStartUpType.None;

            switch (value)
            {
            case JsonStartUpForeground:
                startUp = AppxStartUpType.Foreground;
                break;

            case JsonStartUpBackground:
                startUp = AppxStartUpType.Background;
                break;
            }
            return(startUp);
        }
 public AppReportedState(
     string packageFamilyId,
     string packageFamilyName,
     string version,
     AppxStartUpType startUp,
     string installDate,
     int errorCode,
     string errorMessage,
     JsonReport report)
 {
     this.packageFamilyId   = packageFamilyId;
     this.packageFamilyName = packageFamilyName;
     this.version           = version;
     this.startUp           = startUp;
     this.installDate       = installDate;
     this.errorCode         = errorCode;
     this.errorMessage      = errorMessage;
     this.report            = report;
 }
        private async Task <CommandStatus> InstallAppAsync(
            AppInfo installedAppInfo,
            Version installedAppVersion,
            string connectionString,
            IDictionary <string, AppInfo> installedApps,
            AppxManagementDataContract.AppDesiredState desiredState)
        {
            Logger.Log("Processing install request for " + desiredState.packageFamilyId, LoggingLevel.Verbose);

            if (installedAppInfo == null)
            {
                // It is a new application.
                Logger.Log("    Can't find an installed version... Installing a fresh copy...", LoggingLevel.Verbose);
                await InstallAppFromAzureAsync(installedAppInfo, connectionString, desiredState, false /*not self update*/);       // ---> InstallAppFromAzureAsync
            }
            else
            {
                Windows.ApplicationModel.PackageId thisPackage = Windows.ApplicationModel.Package.Current.Id;
                Debug.WriteLine("FamilyName = " + thisPackage.FamilyName);
                Debug.WriteLine("Name       = " + thisPackage.Name);

                bool isSelf = desiredState.packageFamilyName == thisPackage.FamilyName;

                // A version of this application is installed.
                Logger.Log("    Found an installed version...", LoggingLevel.Verbose);

                if (desiredState.version == installedAppVersion)
                {
                    Logger.Log("        Same version is installed...", LoggingLevel.Verbose);
                    AppxManagementDataContract.AppReportedState appReportedState = null;

                    if (AppUtils.StartUpTypeToMessage(desiredState.startUp) == installedAppInfo.StartUp)
                    {
                        Logger.Log("        App StartUp is the same: desired = " + desiredState.startUp, LoggingLevel.Verbose);

                        appReportedState = new AppxManagementDataContract.AppReportedState(
                            desiredState.packageFamilyId,
                            desiredState.packageFamilyName,
                            installedAppVersion.ToString(),
                            AppUtils.StartUpTypeFromMessage(installedAppInfo.StartUp),
                            installedAppInfo.InstallDate,
                            0,
                            null,                 // no error
                            JsonReport.Report);
                    }
                    else
                    {
                        Logger.Log("        App StartUp is different: desired = " + desiredState.startUp.ToString() + ", current = " + installedAppInfo.StartUp.ToString(), LoggingLevel.Verbose);

                        switch (desiredState.startUp)
                        {
                        case AppxStartUpType.None:
                        {
                            bool background = installedAppInfo.StartUp == StartUpType.Background;
                            if (background)
                            {
                                Logger.Log("            Removing app from background apps.", LoggingLevel.Verbose);
                                StartupAppInfo startupAppInfo = new StartupAppInfo(desiredState.packageFamilyName, background);
                                await RemoveStartupAppAsync(startupAppInfo);
                            }
                            else
                            {
                                Logger.Log("            App is no longer marked to be start-up app. Change takes effect when another app is set to be the start-up app.", LoggingLevel.Verbose);
                            }
                        }
                        break;

                        case AppxStartUpType.Foreground:
                        {
                            Logger.Log("            Setting app to be the foreground app.", LoggingLevel.Verbose);
                            StartupAppInfo startupAppInfo = new StartupAppInfo(desiredState.packageFamilyName, false /*background*/);
                            await AddStartupAppAsync(startupAppInfo);
                        }
                        break;

                        case AppxStartUpType.Background:
                        {
                            Logger.Log("            Adding app to the background apps.", LoggingLevel.Verbose);
                            StartupAppInfo startupAppInfo = new StartupAppInfo(desiredState.packageFamilyName, true /*background*/);
                            await AddStartupAppAsync(startupAppInfo);
                        }
                        break;
                        }

                        AppxStartUpType appStartUp = await GetAppStartup(desiredState.packageFamilyName);

                        Logger.Log("            Querying returned app startup: " + appStartUp.ToString(), LoggingLevel.Verbose);

                        appReportedState = new AppxManagementDataContract.AppReportedState(
                            desiredState.packageFamilyId,
                            desiredState.packageFamilyName,
                            installedAppVersion.ToString(),
                            appStartUp,
                            installedAppInfo.InstallDate,
                            0,
                            null,                 // no error
                            JsonReport.Report);
                    }

                    _stateToReport[desiredState.packageFamilyId] = appReportedState;
                }
                else if (desiredState.version > installedAppVersion)
                {
                    Logger.Log("        Older version is installed...", LoggingLevel.Verbose);

                    if (!String.IsNullOrEmpty(desiredState.appxSource))
                    {
                        // Trigger the update...
                        await InstallAppFromAzureAsync(installedAppInfo, connectionString, desiredState, isSelf);

                        if (isSelf)
                        {
                            // If isSelf == true, it means that SystemConfigurator will force this application to exit very soon.
                            // Let's stop processing any further desired properties.
                            return(CommandStatus.PendingDMAppRestart);
                        }
                    }
                    else
                    {
                        // Note that store updates are not control through DM desired properties.
                        // Instead, they are triggered by the system scan for all store applications.
                        throw new Error(ErrorCodes.INVALID_DESIRED_APPX_SRC, "Appx package is required to update " + desiredState.packageFamilyName);
                    }
                }
                else
                {
                    // desiredState.version < installedAppVersion
                    Logger.Log("       Newer version is installed...rolling back.", LoggingLevel.Verbose);

                    if (String.IsNullOrEmpty(desiredState.appxSource))
                    {
                        AppxManagementDataContract.AppReportedState appReportedState = new AppxManagementDataContract.AppReportedState(
                            desiredState.packageFamilyId,
                            desiredState.packageFamilyName,
                            installedAppVersion.ToString(),
                            AppUtils.StartUpTypeFromMessage(installedAppInfo.StartUp),
                            installedAppInfo.InstallDate,
                            ErrorCodes.INVALID_DESIRED_APPX_SRC,
                            "Cannot install appx without a source.",
                            JsonReport.Report);

                        _stateToReport[desiredState.packageFamilyId] = appReportedState;

                        throw new Exception("Failed to roll back application version.");
                    }

                    if (isSelf)
                    {
                        // The reverting is implemented as an 'uninstall' followed by an 'install'.
                        // The package to be installed is downloaded in the 'install' step.
                        // So, if we were to revert self to an older version, we would have to:
                        //  - (1) download the old version first,
                        //  - (2) send an atomic uninstall-install request to SystemConfigurator to
                        //        uninstall and follow with an install and re-launch.
                        //  The above two steps are not implemented yet.
                        throw new Error(ErrorCodes.CANNOT_REVERT_DM_APPLICATION, "Reverting to an older version of the device management application (" + desiredState.packageFamilyName + ") is not supported.");
                    }
                    // Note that UninstallAppAsync will throw if it fails - and correctly avoid launching the install...
                    await UninstallAppAsync(installedAppInfo,
                                            packageFamilyId : desiredState.packageFamilyId,
                                            packageFamilyName : desiredState.packageFamilyName);
                    await InstallAppFromAzureAsync(installedAppInfo, connectionString, desiredState, isSelf);
                }
            }

            return(CommandStatus.Committed);
        }